I'm a beginner in Python and I don't understand the return value

+1 vote
asked Jan 9 by (140 points)

Hi. I'm 15 and I am just new into the world of programming. Just asking, what is the importance in the return value ? What can it be used for ? Can you at least give an example ? Thank you very much.

2 Answers

0 votes
answered Jan 9 by (240 points)

I was learning a little about Python when I was 13 but now I prefer C++. Well, they both have the return value for the same thing.

The return value of a function gives you what you need after running this function. Here is an example:

def getAdditionOfTwoAddends(firstAddend, secondAddend):

    result = firstAddend + secondAddend

    return result

This is a function that calculates the addition result of two addends. You can see the "result" is the final result of the addition, and it is been returned. The returned number is the value that the function getAdditionOfTwoAddens(number, numberhas, so you can use it this way:

additionResult = getAdditionOfTwoAddends(3, 6)

print (additionResult)

The "result" returned by the function goes to the "additionResult". You'll could use it for other things.

You may ask me: - Why I can't put the print in the function and do not return anything? Because this way you'll can use it for more thing and not just to print it.

Example:

x = getAdditionOfTwoAddends(3, 6)

y = getAdditionOfTwoAddends(7, 9)

print (x * y)

This way you can use it however you want without printing anything. And also the result of x and y are saved.

Anything ununderstandable reply me :)

============END============

I came here was to ask a question XD but saw your question haha

0 votes
answered Jan 17 by (140 points)
Return values is very useful when the function has some calculation / task to perform and give the result value to a variable. for further calculation or tasks. for example as below, to add to variables; even if we did not mention any return statement; "return None" is appended by default

eg:

#in below we return / assign the result of the addition to a variable

def sum(a,b):

       return (a+b)

 sumVal=sum(5,4) #ans: 9
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...