Skip to main content
Engineering LibreTexts

6.5: Return Values

  • Page ID
    117560
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \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}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)
    Learning Objectives

    By the end of this section you should be able to

    • Identify a function's return value.
    • Employ return statements in functions to return values.

    Returning from a function

    When a function finishes, the function returns and provides a result to the calling code. A return statement finishes the function execution and can specify a value to return to the function's caller. Functions introduced so far have not had a return statement, which is the same as returning None, representing no value.

    Checkpoint: Returning a value from a function
    Concepts in Practice \(\PageIndex{1}\): Using return statements

    What is returned by calc_mpg(miles, gallons)?

    def calc_mpg(miles, gallons):
      mpg = miles/gallons
      return mpg
    
    1. mpg
    2. None
    3. Error
    Answer

    a. calc_mpg() specifies mpg to be returned with the return statement.

    Concepts in Practice \(\PageIndex{2}\): Using return statements

    What is returned by calc_sqft()?

    def calc_sqft(length, width):
      sqft = length * width
      return
    
    1. sqft
    2. None
    3. Error
    Answer

    b. return is a valid statement to end the function and return to the calling code without a specified value. calc_sqft() should return sqft with the line return sqft.

    Concepts in Practice \(\PageIndex{3}\): Using return statements

    What is the difference between hw_1() and hw_2()?

    def hw_1():
      print("Hello world!")
      return
    
    def hw_2():
      print("Hello world!")
    
    1. hw_1() returns a string, hw_2() does not
    2. hw_1() returns None, hw_2() does not
    3. no difference
    Answer

    c. If the return value or return statement is not specified, the function returns None .

    Using multiple return statements

    Functions that have multiple execution paths may use multiple return statements. Ex: A function with an if-else statement may have two return statements for each branch. Return statements always end the function and return control flow to the calling code.

    In the table below, calc_mpg() takes in miles driven and gallons of gas used and calculates a car's miles per gallon. calc_mpg() checks if gallons is 0 (to avoid division by 0), and if so, returns -1, a value often used to indicate a problem.

    def calc_mpg(miles, gallons):
      if gallons > 0:
        mpg = miles/gallons
        return mpg
      else:
        print("Gallons can't be 0")
        return -1
        
    car_1_mpg = calc_mpg(448, 16)
    print("Car 1's mpg is", car_1_mpg)
    car_2_mpg = calc_mpg(300, 0)
    print("Car 2's mpg is", car_2_mpg)
    
    Car 1's mpg is 28.0
    Gallons can't be 0
    Car 2's mpg is -1
    
    Table 6.1: Calculating miles-per-gallon and checking for division by zero.
    Concepts in Practice \(\PageIndex{4}\): Multiple return statements

    What does yarn_weight(3) return?

    def yarn_weight(num):
      if num == 0:
        return "lace"
      elif num == 1:
        return "sock"
      elif num == 2:
        return "sport"
      elif num == 3:
        return "dk"
      elif num == 4:
        return "worsted"
      elif num == 5:
        return "bulky"
      else:
        return "super bulky"
    
    1. "lace"
    2. "dk"
    3. "super bulky"
    Answer

    b. num == 3 is True , so the return "dk" statement executes.

    Concepts in Practice \(\PageIndex{5}\): Multiple return statements

    What is the output?

    def inc_volume(level, max):
      if level < max:
        return level
        level += 1
      else:
        return level
    
    vol1 = inc_volume(9, 10)
    print(vol1)
    vol2 = inc_volume(10, 10)
    print(vol2)
    
    1. 9
      10
      
    2. 10
      10
      
    3. 10
      11
    Answer

    a. For inc_volume(9,10), return level returns 9 , and level += 1 is never reached.

    Using functions as values

    Functions are objects that evaluate to values, so function calls can be used in expressions. A function call can be combined with other function calls, variables, and literals as long as the return value is compatible with the operation.

    Checkpoint: Using function calls in expressions
    Concepts in Practice \(\PageIndex{6}\): Using function values

    What is the updated value of bill?

    def tax(total):
      return .06 * total
    
    def auto_tip(total):
      return .2 * total
    
    bill = 100.0
    bill += tax(bill) + auto_tip(bill)
    
    1. 26.0
    2. 126.0
    Answer

    b. bill += tax(bill) + auto_tip(bill) evaluates to bill += 6.0 + 20.0 to bill = 100.0 + 6.0 + 20.0 to 126.0.

    Concepts in Practice \(\PageIndex{7}\): Using function values

    What is the value of val2?

    def sq(num):
      return num * num
    
    def offset(num):
      return num - 2
    
    val = 5
    val2 = sq(offset(val))
    
    1. 9
    2. 23
    Answer

    a. val2 = sq(offset(5)) evaluates to val2 = sq(3) to 9.

    Try It: Estimated days alive

    Write a function, days_alive(), that takes in an age in years and outputs the estimated days the user has been alive as an integer. Assume each year has 365.24 days. Use round(), which takes a number and returns the nearest whole number.

    Then write a main program that reads in a user's age and outputs the result of days_alive().

    Given input:

        21

    The output is:

        You have been alive about 7670 days.
    Interactive Code
     
     
    Input

    21

    Answer

    def days_alive(age):
    return round(age * 365.24)

    user_age = int(input())
    days = days_alive(user_age)
    print("You have been alive about", days, "days.")

    Try It: Averaging lists

    Write a function, avg_list(), that takes in a list and returns the average of the list values.

    Interactive Code
    # Define avg_list()
    
    list1 = [34, 29, 77, 30, 48, 92]
    print(list1)
    print(f"Avg is: {avg_list(list1):.1f}")
    list2 = [4, 2, 12, 8, 8]
    print(list2)
    print(f"Avg is: {avg_list(list2):.1f}")
     
    Answer

    def avg_list(val_list):
    sum = 0
    count = 0
    for val in val_list:
    sum += val
    count += 1
    return sum / count

    list1 = [34, 29, 77, 30, 48, 92]
    print(list1)
    print(f"Avg is: {avg_list(list1):.1f}")
    list2 = [4, 2, 12, 8, 8]
    print(list2)
    print(f"Avg is: {avg_list(list2):.1f}")


    This page titled 6.5: Return Values was last modified on Fri, 24 Jul 2026 08:49:23 GMT and is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.