+ 1

Help me on "return"

Guys i didn't understand "return" in the def function

2nd Dec 2018, 3:58 PM
ANTIDOTE
ANTIDOTE - avatar
7 Antworten
+ 8
It actually makes the function return a value, so you can assign it to a variable for example. If you don't use 'return', the function will do its job, but won't assume any value on its own: def fun1(): a = 10**2 def fun2(): a = 10**2 return a b1 = fun1() b2 = fun2() print(b1, b2) >>> None, 100
2nd Dec 2018, 4:14 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 7
Sure. You define functions to do something for you. It can either be a thing of its own, like opening a file or printing something or some transformation done on values, articularly on values given as arguments. So when a code calls this function with provided arguments, it goes inside the function and executes it. When its done, it exist the function and goes back to the code's main part. And if you want to transfer the value calculated by this function, you have to explicitly use the 'return' clause. Otherwise the calculated value "stays inside" the function and is not "seen" by the rest of the code. Take a look at my earlier example -- both functions do the same, but only fun2() returns the value so it can be assigned to b2. fun1() just does the job and exits without "remembering" to pass it outside. This is why b1 is assigned None. Read more here: https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2287
2nd Dec 2018, 9:28 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 3
ANTIDOTE Actually, to make the *value* produced by the function to be seen by the rest of the code (if assigned to a variable).
4th Dec 2018, 4:25 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 1
When you want to have output you use the print function,but when you are defining a function,you must use the return to have an output. Return yields the function output
2nd Dec 2018, 4:09 PM
Ali Rayat
Ali Rayat - avatar
+ 1
kuba THANK' A LOT <3
5th Dec 2018, 8:51 PM
ANTIDOTE
ANTIDOTE - avatar
0
thank's guys but kuba can you explain it to me more ?
2nd Dec 2018, 8:52 PM
ANTIDOTE
ANTIDOTE - avatar
0
so the return is used make the function seen by the hole code?
4th Dec 2018, 8:16 AM
ANTIDOTE
ANTIDOTE - avatar