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

0 votes
asked Jan 9 by Joshua Hao Jie Lau (170 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.

4 Answers

+4 votes
answered Jan 9 by 刘子铭 (620 points)
selected Mar 9 by Joshua Hao Jie Lau
 
Best answer

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

commented Mar 9 by Joshua Hao Jie Lau (170 points)
Thanks so much ! I finally understand why is the return value is important ! Although not very expert in it, I will try my best to understand and learn more about this thing. Thanks for the help by the way !
0 votes
answered Jan 17 by swanand (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
0 votes
answered Mar 3 by anonymous
its good for ur future by learning pythn it built ur carreir
0 votes
answered Mar 4 by wyattbiker (240 points)

The word 'return' is confusing for newbies. It has nothing to do with returning an item or information back to the store :)

A 'return' value, or result, in most programming languages, just means the 'answer' a function gives. This answer is then stored and used further in your program.

Lets say you find a function called sqrt(number) that you want to use in your code. The docs could state, sqrt(number) returns the square root of number. It would be the exact thing as saying sqrt(number) answers the square root of number

E.g.

r1 = sqrt(81)  <-- Here the return value from sqrt(81) is stored into a variable r1
r2 = r1 * 8.25  <-- Here the r1 value is used to do more calculations.   

Not all functions return values.  Usually these kind functions have side effects only. For example, print('sometext') would print sometext to the screen, but return no meaningful value.

So p1=print('sometext') would be meaningless because p1 would probably have nothing stored in it.

Of course you can build your own functions and have them return values. Keep in mind that a return also means the end of the function. 

Disclaimer: in programming there are always exceptions to everything. Good luck.

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.
...