0
How to use "return" statement in python.
8 odpowiedzi
+ 2
K
+ 1
I have saw that
+ 1
I don't get it
+ 1
I don't get the return statement at all
+ 1
Oh I struggled with the concept at first too but it's actually quiet simple.
So, when you define a function in python you want to use it for something right?! For instance, you want the function to take an integer x and give you back it's square :
def spr(x):
return (x ** 2)
y = sqr(2)
print(y)
# this will print 4
return tells the function "this is the value you must give back (aka must return)" so in the exemple above sqr(2) will return 4 and this value is assigned to y and then printed.
By default , if you don't use return the function will return None as a value. For exemple, this is the case for functions that just prints something to the screen :
def prt():
print('no return in this function')
y = prt()
print(y)
# this will print None on the screen because prt() returns None by default which is assigned to y and then printed
I hope I helped ,although as Ipang said you should play with it a bit to better comprehend to notion.
0
See "Functions and Modules" chapter
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2287/
0
Can you explain what you have understood clearly and what you didn't about `return` statement?
0
Okay buddy,
Well, I'll tell you what. Try to read that chapter again carefully, and play with the "Try It Yourself" examples first. After that, if you still don't understand about it, we can talk again. How about that?