A function, in a mathematical sense, is often described as a mapping
from domain values to range values. Given a domain value, the function
returns the matching range value. If we think of the square root function,
it maps a positive number,
n
, to another number,
s
, such that
s
2 =
n
. If we think of multplication as a function, it
maps a pair of values,
a
and
b
,
to a new value,
c
, such that
c
is the product of
a
and
b
.
In Python, this narrow definition is somewhat relaxed. Python lets
us create functions which do not need a domain value, but create new
objects. It also allows us to have functions that don't return values, but
instead have some other effect, like reading user input, or creating a
directory, or removing a file.
What We Provide. In Python, we create a new function by providing three pieces of
information: the name of the function, a list of zero or more variables,
called parameters, with the domain of input
values, and a suite of statements that creates the output values. This
definition is saved for later use. We'll show this first in the section called “Function Definition: The
def
and
return
Statements”.
Typically, we create function definitions in script files because we
don't want to type them more than once. Almost universally, we
import
a file with our function definitions so we can
use them.
We use a function in an expression by following the function's name
with ()
's. The Python interpreter evaluates the argument
values in the ()
's, then applies the function. We'll show
this second in the section called “Function Use”.
Applying a function means that the interpreter first evaluates all
of the argument values, then assigns the argument values to the function
parameter variables, and finally evaluates the suite of statements that
are the function's body. In this body, any
return
statements define the resulting range value for the function. For more
information on this evaluate-apply cycle, see The Evaluate-Apply Cycle.
Namespaces and Privacy. Note that the parameter variables used in the function definition,
as well as any variables in a function are private to that function's
suite of statements. This is a consequence of the way Python puts all
variables in a namespace. When a function is being evaluated, Python
creates a temporary namespace. This namespace is deleted when the
function's processing is complete. The namespace associated with
application of a function is different from the global namespace, and
different from all other function-body namespaces.
While you can change the standard namespace policy (see the section called “The
global
Statement”,) it generally will do you more harm than
good. A function's interface is easiest to understand if it is only the
parameters and return values and nothing more. If all other variables are
local, they can be safely ignored.
Terminology: argument and parameter. We have to make a firm distinction between an argument
value
, an object that is created or updated during
execution, and the defined parameter
variable
of a
function. The argument is the object used in particular application of a
function; it may be referenced by other variables or objects. The
parameter is a variable name that is part of the function, and is a
local variable within the function body.