print() inside a function versus return

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

print() inside a function versus return

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A function has print(a + b) as its last line and no return. What does result = add(2, 3) store in result?
A None
B 5
2. Which function lets you store its answer in a variable and use it later?
A def double(n): print(n * 2)
B def double(n): return n * 2
3. What does total = add(2, 3) + 10 do if add() only prints a + b and has no return?
A It sets total to 15
B It causes a TypeError, because None + 10 is not allowed
4. Which line puts the result of square(4) into a variable named answer?
A answer = square(4)
B print(square(4)) = answer
5. True or false: print() inside a function sends the value back to the line that called it.
A False
B True

Match the Pairs

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

Left columnRight column
1def add(a, b): return a + b Aage is stored as None because get_age() prints 13 but does not return it. Use return 13 instead.
2def add(a, b): print(a + b) Btotal = add(2, 3)
3Storing the result of a function that returns a value in a variable COnly shows the sum on screen, and the function gives back None
4Using a returned result inside a calculation Dsquare() only prints and gives back None, so adding 1 to None causes a TypeError. Use return n * n instead of print(n * n).
5def square(n): print(n * n) answer = square(4) + 1 Edouble = add(2, 3) * 2
6def get_age(): print(13) age = get_age() FHands the sum back to the caller so it can be stored or used in a calculation

Definitions

return

Fill in the Blanks

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

1.
def total(a, b): ______ a + b print(total(2, 3))
2.
def hi(): print("Hi") print(hi() == ____)
3.
def cube(n): return n ** 3 volume = ____(3)
4.
def double(n): ______ n * 2 print(double(4) + 1)
5.
def greet(): _____("Hello!") greet()

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 total = add(2, 3)
D print(total)
Question 2
A def to_f(c):
B print(c * 9 / 5 + 32)
C warm = to_f(25) > 70
D print(warm)
Question 3
A def add_bonus(n):
B return n + 5
C add_bonus(10)
D print(score)
Question 4
A def cube(n):
B return n * n * n
C x = cube(3)
D return x
Question 5
A def add(a):
B return a + b
C total = add(4, 6)
D print(total)

Answer Key

print() inside a function versus return
Section A: Quick Questions
Q1
A — None
Q2
B — def double(n): return n * 2
Q3
B — It causes a TypeError, because None + 10 is not allowed
Q4
A — answer = square(4)
Q5
A — False
Section B: Match the Pairs
Q1
def add(a, b): return a + b → Hands the sum back to the caller so it can be stored or used in a calculation
Q2
def add(a, b): print(a + b) → Only shows the sum on screen, and the function gives back None
Q3
Storing the result of a function that returns a value in a variable → total = add(2, 3)
Q4
Using a returned result inside a calculation → double = add(2, 3) * 2
Q5
def square(n): print(n * n) answer = square(4) + 1 → square() only prints and gives back None, so adding 1 to None causes a TypeError. Use return n * n instead of print(n * n).
Q6
def get_age(): print(13) age = get_age() → age is stored as None because get_age() prints 13 but does not return it. Use return 13 instead.
Section C: Definitions
Q1
return — Hands a value back to the caller so it can be stored in a variable or used in a calculation. print() inside a function only shows the value on screen, and the function then gives back None.
Section D: Fill in the Blanks
Q1
def total(a, b): return a + b print(total(2, 3))
Q2
def hi(): print("Hi") print(hi() == None)
Q3
def cube(n): return n ** 3 volume = cube(3)
Q4
def double(n): return n * 2 print(double(4) + 1)
Q5
def greet(): print("Hello!") greet()
Section E: Spot the Error
Q1
B — print() only shows the sum on screen and the function then gives back None. So total holds None, not 5. The line should be 'return a + b' so the value is handed back and can be stored.
Q2
B — print() shows the temperature but the function returns None. Comparing None > 70 causes a TypeError. Use 'return c * 9 / 5 + 32' so the result can be used in the comparison.
Q3
C — The returned value is thrown away because it is not stored in a variable. So score is never created and print(score) gives a NameError. It should be 'score = add_bonus(10)'.
Q4
D — return can only be used inside a function. Outside one it causes a SyntaxError. To show the value on screen at the main level, use print(x).
Q5
A — The function body uses b, but b is not listed as a parameter, so the call add(4, 6) fails. The line should be 'def add(a, b):' so both values can be passed in and the sum returned.