Functions – Return Values

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Functions – Return Values

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does return do?
A Prints a value
B Sends a value back to the caller
2. def add(a, b):\n return a + b\nresult = add(3, 5)\nWhat is result?
A 8
B None
3. What's the difference between return and print?
A return sends data back, print only displays
B They are the same
4. What does a function return if there's no return statement?
A None
B 0
5. def double(n):\n return n * 2\nprint(double(5))\nWhat prints?
A 5
B 10

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1def double(n): return n * 2 print('done') AOutput: 8
2def greet(): print('Hi') result = greet() print(result + '!') BError: greet() returns None
3def double(n): return n * 2 result = double(5) CError: Code after return unreachable
4def is_even(n): return n % 2 == 0 DValid: result stores 10
5def double(n): return n * 2 print(double(4)) EOutput: Hello None
6def hi(): print('Hello') print(hi()) FOk: Returns True or False

Definitions

return

Fill in the Blanks

Complete each line of code by filling in the missing parts.

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

Shade in the letter of the line that contains an error.

Question 1
A def add(a, b):
B print(a + b)
C
D result = add(3, 5)
E print(result)
Question 2
A def double(n):
B result = n * 2
C return result
D print('Done')
E print(double(5))
Question 3
A def add(a, b):
B total = a + b
C return total
D print('Adding...')
E print(add(3, 5))
Question 4
A def add(a, b):
B total = a + b
C Return total
D print('Adding...')
E print(add(3, 5))
Question 5
A def double(n):
B result = n * 2
C return(result)
D print('Done')
E print(double(5))

Answer Key

Functions – Return Values
Section A: Quick Questions
Q1
B — Sends a value back to the caller
Q2
A — 8
Q3
A — return sends data back, print only displays
Q4
A — None
Q5
B — 10
Section B: Match the Pairs
Q1
def double(n): return n * 2 print('done') → Error: Code after return unreachable
Q2
def greet(): print('Hi') result = greet() print(result + '!') → Error: greet() returns None
Q3
def double(n): return n * 2 result = double(5) → Valid: result stores 10
Q4
def is_even(n): return n % 2 == 0 → Ok: Returns True or False
Q5
def double(n): return n * 2 print(double(4)) → Output: 8
Q6
def hi(): print('Hello') print(hi()) → Output: Hello None
Section C: Definitions
Q1
return — Sends a value back to the code that called the function.~~Return value
Section D: Fill in the Blanks
Q1
def add(a, b): return a + b result = add(3, 5)
Q2
def double(n): return n * 2 print(double(5))
Q3
def square(n): return n ** 2
Q4
def greet(name): return 'Hello ' + name msg = greet('Al')
Q5
def is_even(n): return n % 2 == 0
Section E: Spot the Error
Q1
B — Using print instead of return: result will be None because print doesn't send back a value
Q2
D — Code after return never runs: move print before return
Q3
C — return must be indented inside the function
Q4
C — return must be lowercase: Return is not valid Python
Q5
C — return is a keyword, not a function: parentheses are unnecessary (but this technically works)