With return
, you can assign to a variable:
def my_funct(x):
return x+1
You can then do y = my_funct(5)
. y
now equals 6.
To help describe it, think of a function to be a machine (similar to what they use in some math classes). By plugging in the variables (in this case, x
), the function outputs (or returns
) something (in this case, x+1
). The variables are the input, and the return
gives the output.
However, with print
, the value is just shown on the screen.
If you change the function to:
def my_funct(x):
print(x+1)
And then do y = my_funct(x)
, y
equals None
, because print()
does not return anything.
Using the machine metaphor, you plug in the variables (again, x
) but instead of outputting something, it just shows you what it equals (again, x+1
). However, it does not output anything.