10.2 Functions as Expressions: Obtaining/Using a Function’s Return Value

Function calls can be used as expressions when the called function returns data (i.e., yields data in the form of function output). Like all other expressions, they have a value. The value of a function call is the function's return value, so we can use these function calls anywhere that we use values.

One of the most important things we can do with the value returned by a function call is to store it in a variable. After all, computers don’t remember anything unless we tell them to, and what would be the point of calculating some value only to forget about it immediately? We’ve already seen an example of this, but here it is again:

The function max() returns the largest number from among its arguments. Because the function call result is a value, we can put the function call itself on the right-hand side of an assignment statement. The resulting value will become associated with the variable biggest_number, which we can then use however we like.

We can also use function calls as operands of an operator, like so:

The code above will print the value 15 to the console because the assignment is the last operation to occur in a Python statement; the expression to the right of the = sign gets evaluated first. The first function call to max() produces the value 5, and the second call produces the value 10. 5 + 10 is 15, so this is the value that gets associated with the variable sum_of_biggest.

We can also use function calls as arguments for another function call. We'll do that here with the min() function, which is the same as max() except that it returns the smallest of its arguments rather than the biggest:

We're using function calls to min() as the 3rd and 4th arguments to the line() function in the code above. As a result, Processing draws a line from coordinate (0, 0) to wherever the mouse is. However, if one of the mouse coordinates is higher than 50, the value 50 will be used instead of the mouse’s actual location due to the min() function calls. The consequence is a program where the user utilizes the mouse to draw a line starting from the top left corner of the canvas, but that line is restricted to a 50x50 area.