+ 2

I do not understand the "return" statement. Please help

10th Feb 2017, 7:47 AM
Vaibhav Singh
Vaibhav Singh - avatar
6 Respuestas
+ 15
def func(x): x = x * x return x print(x + x) func(5) will output 25 since any lines of code after return statement are ignored...
10th Feb 2017, 7:52 AM
Daniel Hisa (1,2,3, Let'sCode!)
Daniel Hisa (1,2,3, Let'sCode!) - avatar
+ 4
the return of the function is what you get back when you call it. for example def add(x,y): c=x+y return c that is a function.. if I call it to add 2 numbers, such as add(2,3) it will return c, which is the sum of 2+3. I could assign it to a variable sum=add(2,3) sum is now 5 because the function returned c which is 5
10th Feb 2017, 7:52 AM
LordHill
LordHill - avatar
10th Feb 2017, 7:57 AM
Mario L.
Mario L. - avatar
+ 1
A return statement can return a value to the calling function and it also terminates the execution of the function, meaning once the return statement has been called the lines that follow the statement will not be executed.
13th Feb 2017, 7:16 AM
Yoliswa Magibili
Yoliswa Magibili - avatar
0
Also return, returns the result in the memory, and do not print it to console by default.
10th Feb 2017, 4:13 PM
fkamaliada
fkamaliada - avatar
0
why do we not return a print directly to the console? is it just bad practice or is there an actual reason?
10th Feb 2017, 8:20 PM
LordHill
LordHill - avatar