Chapter 4 Functions

4.4 Return Statements

Return and conditionals. The custom function sum mentioned before uses a single return statement to specify the return value of function calls. The return statements can be used more flexibily in dynamic control flows, achieving much more powerful functionalities. The following example shows how return statements can be used in branching control flows.

In the example above, three return statements are written in the function posneg, all being embedded in an if … elif … else statement. There are three branches in this if statement. However, depending on the input value x, only one branch is executed at an execution of the statement. As a result, the return statement in the corresponding branch will be executed, giving a dynamic return value for the function call expression.

Because the return statement terminates the execution of a function immediately, no more statements in the function body will be executed after the return statement is executed. The fact can be used to simplify functions. For example, the function posneg above can also be written as:

In the example above, there are three return statements in the function posneg. The first two return statements are each embedded in an if statement, and the last return statement is used unconditionally. In a dynamic control flow, if the value of the input argument x is positive, the return statement in the first if statement will be executed, which terminates the function call without the second if statement being reached. Similarly, if the value of x is negative, the return statement in the second if statement will be executed, which terminates the function call wihtout the last return statement being reached. Consequently, only when the value of x is neither positive nor negative will the last return statement be executed.

Note, however, the fact that posneg can be written in the way above is all because the return statement terminates a function call.

If the posneg function is changed to a procedure, which prints out the messages ‘positive’, ‘negative’ and ‘zero’ rather than returning the strings, the second way to write the function body above will cause errors.

The second posneg function above will always print the message ‘zero’, because the last return statement is unconditional. The execution of the print statements does not terminate the function call.

The difference between return and print statements in a function may seem confusing, especially when the function is called in IDLE: both prints out the value of an expression. However, a more careful look can reveal that the two statements are essentially irrelevant to each other. When the posneg functions with return statements are called, IDLE displays the return values as the values of the function call expression posneg(); but when the posneg functions with the print statements are called, the messages are printed when the function body is executed, and the return values of the function calls, which are None, are not displayed by IDLE.

Check your understanding

© Copyright 2024 GS Ng.

Next Section - 4.5 Identifier Scopes