The Tutorial:
 
 


    Lesson 4: Python Functions

    Functions provide the options of code reuse and decomposition of programs into managable parts [ 4, 2: 98].  In Python, function definitions have the syntax:

      def identifier (argument_1, argument_2, ..., argument_n):

      # Do something
      return value

    The def reserved word defines the function and the return reserved word sends a value back to the calling procedure.  Functions are not required to have a return value.   Therefore, the return is optional.  List and dictionary arguments are passed by reference, meaning that any change to their values remains after the function terminates.  All other variable types are passed by value, and changes made to the variables are not carried beyond the local scope of the function.

    Function calls have the syntax:

      value = identifier(argument_1, argument_2, ..., argument_n)

    Typically, arguments are passed as positionals, and are matched left to right.  They can be passed as keywords with the function arguments assigned to formal parameters in the function call.   Additionly, arguments can be omitted if the function definition supplies default values. Code Example 4.1 demonstrates the typical variations of function calls.

    Code Example 4.1: 
    #!/usr/bin/python

    # Print the required header that tells the browser how to render the text.
    print "Content-Type: text/plain\n\n"

    # Define a function with default argument 'age' = 21.
    def display_vars (first_name, last_name, age = 21):
    print (first_name, last_name, age)

    # Call the function with all positionals.
    display_vars("John", "Smith", 22)

    # Call the function with all keywords.
    display_vars(last_name = "Smith", first_name = "John", age = 22)

    # Call the function with incomplete args; default age used.
    display_vars("John", "Smith")

    A Note on Variable Scope

    Variables in Python can have one of three scopes: local, global, and built-in.  Local variables are specific to their functions.  Global variables are accessible anywhere in a program.  Built-in variables are those defined in the Python language. An important aspect of scope in Python is that local names are detected statically.  This means that a undefined local variable is not looked up in the calling procedure; it obtains its definition from the enclosing module [2: 117].

    Code Example 4.2 demonstrates this feature.

    Code Example 4.2: 
    #!/usr/bin/python

    # Print the required header that tells the browser how to render the text.
    print "Content-Type: text/plain\n\n"

    # Define a global variable gender = "female".
    gender = "female"

    # Define a function.
    def display_vars (first_name, last_name, age):
    print (first_name, last_name, age, gender)

    # Define a function that calls 'display_vars' with intended local 'gender'.
    def go_display():
    gender = "male"
    display_vars("John", "Smith", 22)

    # Call the function 'go_display'.
    go_display()

    Note that the Python interpreter did not look up the value of gender dynamically at runtime from the calling procedure.  Instead, it found the value of gender by recursing the static chain of the compiled code, and therefore selected the logically incorrect value.

    To Lesson 5 ...

 © 2000 Department of Computer Science, University of Virginia