Last Updated: Sunday 24th March 2013

Virtually every programming language has functions and procedures, a way of separating out a block of code that can be called many times from different places in your program, and a way to pass parameters into them. Python is no different, so we'll quickly run over the standard stuff that most languages have, then take a look at some of the cool stuff Python has to offer.

Positional Function Parameters in Python

Here's a really simply function:

When used, we get the following:

This function has 3 positional parameters (each one gets the next value passed to the function when it is called – val1 gets the first value (1), val2 gets the second value (2), etc).

Named Python Functional Parameters (with defaults)

Python also supports named parameters, so that when a function is called, parameters can be explicitly assigned a value by name. These are often used to implement default, or optional, values. For example:

Using the function gives us the following:

In this example, calcSum is optional - if it's not specified when you call the function, it gets a default value of True.

Potential Python Function Parameter Problems

One thing to keep in mind is that default values are evaluated when the function is compiled, which is an important distinction if the value is mutable. The behaviour below is probably not what was intended:

When using the function:

This happens because the default value (an empty list) was evaluated once, when the function was compiled, then re-used on every call to the function. To get an empty list on every call, the code needs to be written like this:

When using the function, we get:

Python Function Parameter Order

Unlike positional parameters, it doesn't matter what order named parameters are specified in:

When used, we get the following:

Unusually, Python also allows positional parameters to be specified by name:

When used, we get the following:

Here's a more complex example:

When used, we get the following:

This seems really confusing, but the key to understanding how it works is to recognize that the function's parameter list is a dictionary (a set of key/value pairs). Python matches up the positional parameters first, then assigns any named parameters specified in the function call.

Variable Python Function Parameter Lists

The Python coolness really kicks in when you start to look at variable parameter lists. You can write your functions without even knowing what parameters will be passed in!

In the function below, the asterisk in front of the vals parameter means any other positional parameters.

When using the function, we get:

The first positional value we specify in the function call (10) is given to the first parameter in the function (cutoffVal), then all the remaining positional values are put in a tuple and assigned to vals. We then iterate through these values, looking for any that are less than the cutoff value.

We can also do the same kind of thing with named parameters. A double asterisk in front of the dict parameter below means any other named parameters. This time, Python will give them to us as key/value pairs in a dictionary.

When using the function, we get:

Note in the last example that the values are not printed out in the same order that they were specified in the function call. This is because these extra named parameters are passed through in a dictionary, which is an un-ordered data structure.

A Real World Example

So, what would you use all this for? As an example, it's common for programs to generate messages from a template that has placeholders for values that will be inserted at run-time. For example:

Hello {name}. Your account balance is {1}, you have {2} available credit.

The function below takes such a template and a set of parameters that will be used to replace the placeholders.

Here it is in action: