Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

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

Using return statements

1.
What is returned by calc_mpg(miles, gallons)?
def calc_mpg(miles, gallons):
  mpg = miles/gallons
  return mpg
  1. mpg
  2. None
  3. Error
2.
What is returned by calc_sqft()?
def calc_sqft(length, width):
  sqft = length * width
  return
  1. sqft
  2. None
  3. Error
3.
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

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

Multiple return statements

4.
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"
5.
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
    

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

Using function values

6.
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
7.
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

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.
    

Try It

Averaging lists

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

Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Feb 26, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.