Skip to main content
Engineering LibreTexts

6.1: Return values

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)

    Calling the function generates a return value, which we usually assign to a variable or use as part of an expression.

    e = math.exp(1.0)
    height = radius * math.sin(radians)

    The functions we have written so far are void. Speaking casually, they have no return value; more precisely, their return value is None.

    In this chapter, we are (finally) going to write fruitful functions. The first example is area, which returns the area of a circle with the given radius:

    def area(radius):
        a = math.pi * radius**2
        return a

    We have seen the return statement before, but in a fruitful function the return statement includes an expression. This statement means: “Return immediately from this function and use the following expression as a return value.” The expression can be arbitrarily complicated, so we could have written this function more concisely:

    def area(radius):
        return math.pi * radius**2

    On the other hand, temporary variables like a can make debugging easier.

    Sometimes it is useful to have multiple return statements, one in each branch of a conditional:

    def absolute_value(x):
        if x < 0:
            return -x
            return x

    Since these return statements are in an alternative conditional, only one runs.

    As soon as a return statement runs, the function terminates without executing any subsequent statements. Code that appears after a return statement, or any other place the flow of execution can never reach, is called dead code.

    In a fruitful function, it is a good idea to ensure that every possible path through the program hits a return statement. For example:

    def absolute_value(x):
        if x < 0:
            return -x
        if x > 0:
            return x

    This function is incorrect because if x happens to be 0, neither condition is true, and the function ends without hitting a return statement. If the flow of execution gets to the end of a function, the return value is None, which is not the absolute value of 0.

    >>> print(absolute_value(0))

    By the way, Python provides a built-in function called abs that computes absolute values.

    As an exercise, write a compare function that takes two values, x and y, and returns 1 if x > y, 0 if x == y, and -1 if x < y.

    6.1: Return values is shared under a CC BY-NC 3.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) .

    • Was this article helpful?