We have discussed how to define and call simple functions and how to pass data arguments into a function, but not how to design a function that returns data. A value-returning function is a special type of function that returns one or more values back to the part of the program that called it. The value(s) that are returned can be used like any other value: assigned to a variable, displayed on the screen, used in a calculation etc…
A good way to think about a function is as a “black box” that accepts input (parameters), performs some operation (that cannot be seen) using the input, then produces output. As a programmer, you only need to understand what a function does, what input it requires, and what it returns to be able to use it effectively in your program. You don’t have to know how the function works internally (i.e., what the code looks like). This is the IPO (Input-Processing-Output) model in action!
Creating Your Own Value-Returning Functions
A value-returning function is written in the same way we have been writing simple functions, with one exception: One of the statements in the function must be a return statement of the form:
The expression that follows the return keyword will be sent back to the part of the program that called the function. The expression can be any value, variable, or expression that has a value (such as a mathematical expression). Also, any kind of data can be returned by a function including int, float, str, and bool values.
Without a return statement in a function, it will automatically return a special value called None when the end of the function code block is reached. This is what the simple functions we defined previously have done. None is a special value (like True and False) used to indicate “nothing here”.
Here’s a simple example defining and calling a value-returning function:
1 2 3 4 |
def is_even(number): '''This function accepts a numeric parameter and returns True if the number is even, or False otherwise.''' return (number % 2) == 0 |
Our is_even() function is an example of a Boolean function. As you would expect, a Boolean function is a function that returns either a True or False value. When naming a Boolean function, the name should reflect a simple question with a True or False answer. “Is Even?” clearly has only two possible answers.
One of the unique features of Python is that you can actually return more than one value from a function by specifying multiple expressions separated by commas after the return statement.
Style Rule #6 (revised again!): Function CommentingOnce again we need to improve how we are commenting functions to reflect any return value(s). From now on, every function that you create must include three things:
By including all three of these things in your function comments, it makes it very clear to others reading your code (and perhaps calling your function) what the IPO model is. |
You Try!
-
- Add the heading “1-11 Returning Data from Functions” to your learning journal and answer the following questions:
-
- What does the return keyword do in a function?
- What is a Boolean function?
- What is multi-valued assignment? Give a 1 line example and explain how it works.