#
7.5 Function definitions

A function definition defines a user-defined function object (see section 3.2):

      funcdef:        "def" funcname "(" [parameter_list] ")" ":" suite
      parameter_list: (defparameter ",")* ("*" identifier [, "**" identifier]
                                          | "**" identifier
                                          | defparameter [","])
      defparameter:   parameter ["=" expression]
      sublist:        parameter ("," parameter)* [","]
      parameter:      identifier | "(" sublist ")"
      funcname:       identifier

A function definition is an executable statement. Its execution binds the function name in the current local namespace to a function object (a wrapper around the executable code for the function). This function object contains a reference to the current global namespace as the global namespace to be used when the function is called.

The function definition does not execute the function body; this gets executed only when the function is called.

When one or more top-level parameters have the form parameter `=` expression, the function is said to have ``default parameter values.'' For a parameter with a default value, the corresponding argument may be omitted from a call, in which case the parameter's default value is substituted. If a parameter has a default value, all following parameters must also have a default value -- this is a syntactic restriction that is not expressed by the grammar.7.1

Default parameter values are evaluated when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that that same ``pre-computed'' value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use `None` as the default, and explicitly test for it in the body of the function, e.g.:

      def whats_on_the_telly(penguin=None):
          if penguin is None:
              penguin = []
          penguin.append("property of the zoo")
          return penguin

Function call semantics are described in more detail in section 5.3. A function call always assigns values to all parameters mentioned in the parameter list, either from position arguments, from keyword arguments, or from default values. If the form ```*identifier`'' is present, it is initialized to a tuple receiving any excess positional parameters, defaulting to the empty tuple. If the form ```**identifier`'' is present, it is initialized to a new dictionary receiving any excess keyword arguments, defaulting to a new empty dictionary.

It is also possible to create anonymous functions (functions not bound to a name), for immediate use in expressions. This uses lambda forms, described in section 5.10. Note that the lambda form is merely a shorthand for a simplified function definition; a function defined in a ```def`'' statement can be passed around or assigned to another name just like a function defined by a lambda form. The ```def`'' form is actually more powerful since it allows the execution of multiple statements.

Programmer's note: a ```def`'' form executed inside a function definition defines a local function that can be returned or passed around. Because of Python's two-scope philosophy, a local function defined in this way does not have access to the local variables of the function that contains its definition; the same rule applies to functions defined by a lambda form. A standard trick to pass selected local variables into a locally defined function is to use default argument values, like this:

      # Return a function that returns its argument incremented by 'n'
      def make_incrementer(n):
          def increment(x, n=n):
              return x+n
          return increment

      add1 = make_incrementer(1)
      print add1(3)  # This prints '4'

#### Footnotes

... grammar.7.1
  Currently this is not checked; instead, `def f(a=1, b)` is interpreted as `def f(a=1, b=None)`.