Linux/Unix > Python Essential Reference, Second Edition > 6. Functions and Functional Programming > Parameter Passing and Return Values | See All Titles

< BACK | Make Note | Bookmark | CONTINUE >

155117184014003188065099048180054212144238241179195140058238110137104016132047023109078082

### 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:

    x,y = factor(1243)    # Return values placed in x and y.
    (x,y) = factor(1243)  # Alternate version. Same behavior.

| Last updated on 3/28/2002 Python Essential Reference, Second Edition, © 2002 New Riders Publishing

< BACK | Make Note | Bookmark | CONTINUE >

© 2002, O'Reilly & Associates, Inc.