+ 3

What is the purpose of a return statement in a function?

A return statement in a function looks like this. def test(func, arg): return func(func(arg)) def mult(x) return x * x print(test(mult, 2)) Okay so according to the Python course this is higher order function. Which basically takes other functions as arguments, or return them as results. Which I can understand. However, what is the purpose of the return statement? Now if I comment that out and use pass (Just to see how the code will react) i get None. def test(func, arg): pass #return func(func(arg)) def mult(x) return x * x print(test(mult, 2)) Output>>> None So, is the return statement doing something on the backend, unseen? Now, I read online that return statement returns when function is called. So, is the print function calling the function or the return?

22nd Nov 2019, 1:20 AM
erica price
erica price - avatar
8 Antworten
+ 2
The return creates a result of your function, a result which may be used elsewhere within your code. Print makes the return of the function visible to the user.
22nd Nov 2019, 1:25 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
erica price print is calling your test function which accepts two values. A return statement is always used to get something back from the function you call or else there is no point in calling a function unless it returns or prints something for you.
22nd Nov 2019, 1:25 AM
Avinesh
Avinesh - avatar
+ 2
erica price please forgive me if I make syntax mistake since I do not know Python. But take this for your understanding. For eg- def add(a,b): return a+b print(add(10,20)) Now see the above code where add is a function which is called inside print because you want to print the result that's it. You pass 10 and 20 to add function, so now you are expecting a value back from the function to print it right? That is what the return statement do. In this case it is returning 10+20=30. So your print statement gives 30 as output.
22nd Nov 2019, 1:38 AM
Avinesh
Avinesh - avatar
+ 2
Return is a statement that does 2 things: ~Terminates the function call. ~Returns a value to the position where the function was called.
22nd Nov 2019, 5:07 AM
Seb TheS
Seb TheS - avatar
+ 1
Okay so a function has to be called either by a return statement or print function or both in order to retrieve some output to be shown visibly? And thanks to the both of you Rik Wittkopp and Avinesh.
22nd Nov 2019, 1:28 AM
erica price
erica price - avatar
+ 1
erica price No you cannot call a function using return statement. The return statement is generally the end of a function itself. And if you use return in between the function then your rest of the code inside the function will not execute.
22nd Nov 2019, 1:32 AM
Avinesh
Avinesh - avatar
+ 1
Okay. Thanks Avinesh
22nd Nov 2019, 1:35 AM
erica price
erica price - avatar
0
Thanks Avinesh and Seb TheS that makes sense I am looking to not only learn to code but to learn why. I believe in understanding the code rather than it is what it is. You know? So thanks for the help everyone. More questions to come! 😁
22nd Nov 2019, 12:55 PM
erica price
erica price - avatar