Chapter 4 Functions

4.1 Function Definition

As discussed before, many mathematical functions cannot be computed by using a simple expression. There are cases where the value of a custom function must be obtained by iterative numerical computation. In such cases, the calculation of a return value requires a procedure that consists of multiple statements, typically representing a dynamic control flow.

Instead, Python provides a special statement, the def statement, for the definition of functions that consist of multiple statements. To begin with, consider the following function f:

The syntax of the def statement is illustrated in Fig. 4.1. The first line of a def statement starts with the def keyword, followed by a custom function name, and a comma-separated list of parameters, enclosed in a pair of parenthesess, and finally a colon. After the def line is an indented statement block, which is called the function body. The function body contains one or many return statements, which begin with the return keyword, followed by an expression:

return <expression >

The def statement performs two tasks. First, it defines a function object. Second, it associates the function object with the function name given in the def line.


Fig. 4.1 The def statement

Fig. 4.1 The def statement

In a function call, the function name is used as the identifier for accessing the function object. The parameters defined in the def line are assigned the argument values given in the function call expression, before the function body is executed. During the execution of the function body, if one return statement is executed, the execution of the function body is terminated, with the value of the expression in the return statement being taken as the return value of the function call.

In the example above, the def statement defines a function object with two parameters, and assigns the name f to it. When the call f(1,2) is evaluated, the argument 1 and 2 are assigned to the parameters x and y, respectively, and the function body is executed when the return statement is executed, the function call returns, with the value of x + z, which is 5.

If no return statements are executed before the whole function body is executed, a special return value, None, is taken as the return value of the function call. Here None belongs to a new type, the NoneType. It is the only object that belongs to the type.

The object None is equal to neither 0, nor ‘’, nor False, because NoneType is a different type as compared to a number, the string or the Boolean type. None can be converted into a string, ‘None’, or a Boolean object, False.

In the example above, when the value of None is given to IDLE (i.e. by typing the identifier a directly as an expression in IDLE), nothing is shown on the console. This is different from other objects, for which IDLE display the value. By default, IDLE does not display the None value. However, when given to the print statement, None is first converted into a string, and then shown.

None serves as a special value in Python, representing the meaning ‘nothing’.

Correlated to the None object is a statement that does nothing: the pass statement. The syntax of the pass statement consists of the pass keyword only. It serves as a placeholder where a statement block is required, but nothing needs to be performed.

A simplest function can be one that takes no argument, and performs nothing:

In the example above, the function call f() does not perform anything: the function body is a single pass statement. In this example, the pass statement is necessary, because an indented statement block is required in the definition of a function. Since no return statement is executed during the execution of the function body, the return value of the function call f () is None. Note that when f () is used as a single-line expression, nothing is shown as the value of the expression. This is because IDLE shows no output for the value None, as illustrated earlier.

For a more complex example, consider a function that shows the string ‘Hello, world’ on the console.

In the example above, when the assignment statement a = g() is executed, the expression g() is evaluated first. It is a function call expression, which leads to the invoking of the function body of g. As a result, the string ‘Hello, world’ is printed to the console. The value of the function call expression g() is assigned to the identifier a. Since no return statement is executed, the default return value, None, is returned.

Check your understanding

© Copyright 2024 GS Ng.

Next Section - 4.2 The Dynamic Execution Process of Function Calls