Functions are extremely important in all programming languages both because they encapsulate useful tasks and because they support code reuse. Often they form part of the language itself, and Python has many built-in functions. For example, print is the built-in Python function that generates a string based on the supplied input objects and sends this string to an output. But Python also allows programmers to create their own functions, as a way to improve the structure and readability of their codes.

A Python function consists of an indented code block appearing after a def statment. The def statement specifies the name of the function, together with its input parameters or arguments. A return statement within the function definition may optionally specify a return value; often one thinks of this as the value of the function for the given inputs. If a return value is not specified, or if a return statement is not included in the function definition, then the function returns the Python object None. The function body comprises an independent code block, i.e., the variables in it are not accessible outside of the function.

The syntax of a function typically looks like the following:

def function_name(parameter1, parameter2, ...):
    statement1
    statement2
    return A_value_or_blank

By default, parameters have positional behavior, so in a call to the function, you need to supply values for the parameters in the same order that they were defined. When the function is called, the values in the function call are known as arguments. The parameters we see in the definition of a function are really just placeholders for the arguments; the arguments are the actual values that are used during the function call.

Here is an example showing both a function definition and calls to that function with various arguments.

Default arguments

A default argument is the value that a particular parameter will assume if one is not provided in the function call. It is not necessary for parameters to have default arguments. Any parameter that does have a default value becomes an optional argument when the function is called; arguments with default values must all appear at the end of the argument list for the function, i.e., after the positional arguments, so that the interpreter can assign values correctly. A default argument is demonstrated in the following example:

Let’s execute this function a couple of times and see what the results are:

Keyword arguments

Keyword arguments allow functions to be specifically identified in the argument list, which can make code more readable; this also allows them to be called with arguments appearing in a different order from the defined order, although whether this is a good thing is a matter of taste. Each keyword argument must be preceded with “parametername=” to make it clear which argument corresponds to which parameter.

Let’s now try a few combinations of keyword arguments to the twoargs function, as defined above, and see what happens.

As we can see, any default arguments may still be omitted, and the omitted arguments will assume their default values as usual. Mixtures of keywords and non-keywords are allowed, but the non-keywords must appear first, so their order is meaningful.

Mini-exercise: function with a nested if/else
  • Read and evaluate a number from the keyboard
  • Pass the number to a function
  • Pass the number to a function
 
©  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Inclusivity Statement