[Tutor] 'return'

Alan Gauld alan.gauld@blueyonder.co.uk
Sat May 31 07:43:02 2003


> ok, 'return'

You're coming from a math background so I'll use a 
math analogy.

return is the value returned from a function.

Thus if we define a function in math:

f(x) = x*x

you can assign that to a variable by:

y = f(x)

or even

y = f(2)

so that y now equals 4.

THis translates in Python to

def f(x):
   return x*x

and

y = f(2)

y now stores 4

But if we had used print instead of return
y would store 'None' (the default return value 
from a python function) and the function would 
have printed 4. 

So using return allows your functions to produce a 
value that can be stored by a variable.

Try reading the modules and functions topic in my 
tutor for a slightly different explanation.

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld