Skip to main content

11.7) Arguments


Here is an example of functions with multiple input arguments:

def vol_sphere(radius, pi):
    return 4./3.*pi*(radius**3)

You have several ways of calling the function.

Positional arguments: The simplest is to give a list of the arguments in the right order:

>>> print vol_sphere(1.,3.14)
4.18666666667

The radius will be set to the first argument (4.5) and pi to the second argument (3.14). These are what we call positional arguments.

Keyword arguments: You also have the flexibility to specify the name of the arguments, rather than relying on remembering their position. You do this by using keyword arguments:

>>> print vol_sphere(pi=3.14, radius=1.)
4.18666666667

The order of arguments doesn’t matter in this case.

Mixing positional and keyword arguments: It’s also possible to give some of the arguments in order, followed by the remaining arguments in keywords:

>>> print vol_sphere(1., pi=3.14)
4.18666666667

The radius has to come first in this case.