Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions




Chapter 9. Functions

The heart of programming is the evaluate-apply cycle, where function arguments are evaluated and then the function is applied to those argument values. We'll review this in the section called “Semantics”.

In the section called “Function Definition: The def and return Statements” we introduce the syntax for defining and using a function. We introduce some of the alternate argument forms available for handling optional parameters in the section called “More Features”. Then, in the section called “Providing Argument Values by Keyword” we show how Python can use keyword parameters as well as positional parameters.

In the section called “Object Method Functions” we will describe how to use method functions as a prelude to Part II, “Data Structures”; real details on method functions are deferred until Chapter 21, Classes .

Further sophistication in how Python handles parameters has to be deferred to the section called “Advanced Parameter Handling For Functions”, as it depends on a knowledge of dictionaries, introduced in Chapter 15, Mappings and Dictionaries .

We'll also defer examination of the yield statement until Chapter 18, Generators and the yield Statement . The yield statement creates a special kind of function, one that is most useful when processing complex data structures, something we'll look at in Part II, “Data Structures”.


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.

  Published under the terms of the Open Publication License Design by Interspire