February 2006
Intermediate to advanced
648 pages
14h 53m
English
When a function is invoked, its parameters are passed by reference. If a mutable object (such as a list or dictionary) is passed to a function where it’s then modified, those changes will be reflected in the caller. For example:
a = [1,2,3,4,5]
def foo(x):
x[3] = -55 # Modify an element of x
foo(a) # Pass a
print a # Produces [1,2,3,-55,5]The return statement returns a value from a function. If no value is specified or you omit the return statement, the None object is returned. To return multiple values, place them in a tuple:
def factor(a):
d = 2
while (d <= (a/2)):
if ((a/d)*d == a):
return ((a/d),d)
d = d + 1
return (a,1)Multiple return values returned in a tuple can be assigned to individual variables: ...