Parameter Passing and Return Values
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: ...
Get Python: Essential Reference, Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.