Skip to main content

Section 6.11 Value-Returning Functions and Void Functions

Take a look at the following program:
import math

value_str = input("Enter a value:")
value = float(value_str)
twice_root = 2 * math.sqrt(value)
print("The result is", twice_root)
Lines 3 through 5 all have a function call, and the call returns some value that gets assigned to a variable. We call functions that return a value value-returning functions or fruitful functions. These functions are usually used on the right hand side of an assignment statement or are used in some expression.
Most of the functions you write will be value-returning functions: they will have a return statement that returns a value to the caller.
The last line in the program uses the print function. Unlike the other functions, it doesn’t return a value that we can use in further calculations. These are called void functions.
You can also write your own void functions. They will not have a return statement in them. Consider this program, which uses a function named debug to avoid having to duplicate print statements:
Again, we know that debug is a void function because it does not have a return statement. The debug function may seem to be somewhat unnecessary, but now we can use one of the advantages of functions as described in Section 6.7: ...[I]f you make a change, you only have to make it in one place.
If we were using individual print statements for our debugging, we would have to comment them out one by one throughout the whole program when we were ready to ship the program to our customers. Instead, we can replace the print in the debug function with pass, and the debugging output will no longer show up when we run our program:
If we modify the program and need to debug again, we can un-comment the print statement and get back all of our debugging output with very little effort.

Checkpoint 6.11.1.

    “Value-returning functions” are functions that must…
  • return a value
  • Correct! Value-returning functions yield results in the form of a return value.
  • not return a value
  • Incorrect! Value-returning functions do return a value. Try again.
  • print something
  • Incorrect! Value-returning functions may print something, but they also must do something else. Try again.
  • display something on the screen
  • Incorrect! Value-returning functions may display something on the screen, but they must also do something else. Try again.

Checkpoint 6.11.2.

    “Void functions” are functions that…
  • return a value
  • Incorrect! Void functions don’t return a value. Try again.
  • do not return a value
  • Correct! Void functions don’t return a value.
  • return a variable
  • Incorrect! Void functions don’t return a variable—a variable is a kind of value. Try again.
  • must take parameters
  • Incorrect! Void functions may take parameters, but not always. Try again.
IMPORTANT: If you call a value-returning function and do not store the result of the function in a variable, the return value vanishes into the mist!
def average(a, b):
    return (a + b) / 2
    
average(3.7, 8.9)
This script computes the average of 3.7 and 8.9, but since it doesn’t store the result in a variable or display the result, it is not very useful.
You might be wondering what would happen if you assigned the result of a void function to a variable. If you do (as in line 2 of the following program), you get a special value called None.