1-11 Returning Data from Functions

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:


For efficiency, one could greatly shorten the is_even() function like this:


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.


Notice when we call the get_name() function on line 11 there are two variables on the left of the assignment operator (=).  After this statement executes, the value of the first variable will be assigned to first_name, and the value of the last variable will be assigned to last_name.  This is called a multi-valued assignment.  The number of variables on the left of the = operator must match the number of values returned by the function or an error will occur.

Style Rule #6 (revised again!): Function Commenting

Once 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:

    • Purpose: A brief (1-2 sentence) explanation of what the function does.
    • Parameter(s): Any data to be passed into the function should be explained.
    • Return Value(s): Any return value(s) should be explained.

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!

    1. Add the heading “1-11 Returning Data from Functions” to your learning journal and answer the following questions:
      1. What does the return keyword do in a function?
      2. What is a Boolean function?
      3. What is multi-valued assignment?  Give a 1 line example and explain how it works.