+ 2
I do not understand the "return" statement. Please help
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...
+ 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
+ 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.
0
Also return, returns the result in the memory, and do not print it to console by default.
0
why do we not return a print directly to the console? is it just bad practice or is there an actual reason?