0

How to use "return" statement in python.

7th Jan 2020, 5:58 AM
Tanishq Sangwan
Tanishq Sangwan - avatar
8 ответов
+ 2
K
7th Jan 2020, 7:19 AM
Tanishq Sangwan
Tanishq Sangwan - avatar
+ 1
I have saw that
7th Jan 2020, 7:12 AM
Tanishq Sangwan
Tanishq Sangwan - avatar
+ 1
I don't get it
7th Jan 2020, 7:12 AM
Tanishq Sangwan
Tanishq Sangwan - avatar
+ 1
I don't get the return statement at all
7th Jan 2020, 7:15 AM
Tanishq Sangwan
Tanishq Sangwan - avatar
+ 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.
8th Jan 2020, 11:30 PM
MedG
MedG - avatar
7th Jan 2020, 7:11 AM
Ipang
0
Can you explain what you have understood clearly and what you didn't about `return` statement?
7th Jan 2020, 7:15 AM
Ipang
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?
7th Jan 2020, 7:18 AM
Ipang