tutorial example
Max Erickson
maxerickson at gmail.com
Fri Nov 11 21:09:19 EST 2005
>>> import math
>>> def distance1(x1, y1, x2, y2):
dx = x2 - x1
dy = y2 - y1
dsquared = dx**2 + dy**2
result = math.sqrt(dsquared)
print result
return result
>>> def distance2(x1, y1, x2, y2):
dx = x2 - x1
dy = y2 - y1
dsquared = dx**2 + dy**2
result = math.sqrt(dsquared)
print result
They don't do the same thing here...
>>> distance1(1,2,3,4)
2.82842712475
2.8284271247461903
>>> distance2(1,2,3,4)
2.82842712475
the 'return result' line passes the result object in the function
back to where the function was called. Functions without a return
statement default to returning 'None'. Calling the functions within
a print statement illustrates the difference:
>>> print distance1(1,2,3,4)
2.82842712475
2.82842712475
>>> print distance2(1,2,3,4)
2.82842712475
None
>>>
As you can see, distance2 does not actually return the result of the
calculation to the interactive prompt...
max
More information about the Python-list
mailing list