Functions – Return Values, online

Six activities your students do on their own devices. See who's finished, and their scores.

Functions – Return Values in one minute

What students need to know

The returned value is stored in 'result'. result = 10. Functions can return booleans. n % 2 == 0 is True when n is even.

return passes a value out of the function — print only displays it. add() returns 3+5=8, which is stored in result.

Mistakes students make

  • Using print instead of return: result will be None because print doesn't send back a value.
  • Code after return never runs: move print before return.
  • return must be indented inside the function.
  • return must be lowercase: Return is not valid Python.
PREVIOUS · MODULE 25Functions – Parameters
YOU ARE HERE · MODULE 26Functions – Return Values
NEXT · MODULE 27Functions – Scope

Discover other journeys with this module

What students will learn in this module

Every question in Module 26

Quick questions

  1. What does return do?APrints a valueBSends a value back to the caller
  2. def add(a, b):\n return a + b\nresult = add(3, 5)\nWhat is result?A8BNone
  3. What's the difference between return and print?Areturn sends data back, print only displaysBThey are the same
  4. What does a function return if there's no return statement?ANoneB0
  5. def double(n):\n return n * 2\nprint(double(5))\nWhat prints?A5B10

Key terms

Students type the meaning of:

  • return
  • Return value
  • None
  • return vs print

Match the pairs

def double(n):
    return n * 2
    print('done')
Match it to what it does
def greet():
    print('Hi')
result = greet()
print(result + '!')
Match it to what it does
def double(n):
    return n * 2
result = double(5)
Match it to what it does
def is_even(n):
    return n % 2 == 0
Match it to what it does
def double(n):
    return n * 2
print(double(4))
Match it to what it does
def hi():
    print('Hello')
print(hi())
Match it to what it does

Fill in the blanks

  1. def add(a, b):
        ______ a + b
    
    result = add(3, 5)
  2. def double(n):
        return n * 2
    
    print(______(5))
  3. def square(n):
        return n __ 2
  4. def greet(name):
        ______ 'Hello ' + name
    
    msg = greet('Al')
  5. def is_even(n):
        return n % 2 __ 0

Spot the error

Question 1

Adef add(a, b):B    print(a + b)CDresult = add(3, 5)Eprint(result)

Question 2

Adef double(n):B    result = n * 2C    return resultD    print('Done')Eprint(double(5))

Question 3

Adef add(a, b):B    total = a + bCreturn totalD    print('Adding...')Eprint(add(3, 5))

Question 4

Adef add(a, b):B    total = a + bC    Return totalD    print('Adding...')Eprint(add(3, 5))

Question 5

Adef double(n):B    result = n * 2C    return(result)D    print('Done')Eprint(double(5))