Common function mistakes: print instead of return

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

Common function mistakes: print instead of return

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which function gives back a value the caller can store and use?
A def double(n): return n * 2
B def double(n): print(n * 2)
2. What does x hold after this code runs? def add(a, b): print(a + b) x = add(2, 3)
A None
B 5
3. Why does total = add(2, 3) + 1 crash when add() only prints its answer?
A add() returns None, and None + 1 is a TypeError
B add() returns 5, but Python cannot add 1 to it
4. Which function finds the first even number in a list correctly?
A for n in nums: if n % 2 == 0: print(n)
B for n in nums: if n % 2 == 0: return n
5. This function should return the sum of a list. What is the bug? def total(nums): s = 0 for n in nums: s += n return s
A return should be replaced with print so the loop keeps going
B return is inside the loop, so it stops after the first item

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): print(a + b) total = add(2, 3) Aarea() prints instead of returning, so it gives back None and None + 10 causes a TypeError. Replace print with return.
2def add(a, b): return a + b total = add(2, 3) Bdef total(nums): s = 0 for n in nums: s += n return s
3A return placed inside the loop ends the function on the first pass, so only the first item is handled Cdef make_total(a, b): result = a + b return result
4A local variable only exists inside its function, so the caller must receive its value with return Dtotal becomes 5, because return sends the sum back to the caller
5def area(w, h): print(w * h) big = area(4, 5) + 10 Eresult is a local variable, so it cannot be used outside the function and causes a NameError. Return it and store the answer from the call.
6def make_total(a, b): result = a + b make_total(2, 3) print(result) Ftotal becomes None, because add() prints the sum but never returns it

Definitions

return

Fill in the Blanks

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

1.
def add(a, b): ______ a + b
2.
def show(n): print(n) print(show(3) == ____)
3.
def first(nums): for n in nums: ______ n
4.
def divide(a, b): return a / b print(divide(__, 2))
5.
def area(w, h): return w * h print(____(3, 4))

Spot the Error

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

Question 1
A def take_away(small, big):
B return big - small
C result = take_away(10, 4)
D print(result)
Question 2
A def double(n):
B print(n * 2)
C x = double(5)
D print(x + 1)
Question 3
A def average(nums):
B total = sum(nums)
C avg = total / len(nums)
D print(avg)
E score = average([4, 6, 8])
F print(score + 1)
Question 4
A def cube(n):
B result = n * n * n
C result
D volume = cube(3)
E print(volume + 1)
Question 5
A def add_bonus(mark):
B new_mark = mark + 5
C new = add_bonus(60)
D print(new)

Answer Key

Common function mistakes: print instead of return
Section A: Quick Questions
Q1
A — def double(n): return n * 2
Q2
A — None
Q3
A — add() returns None, and None + 1 is a TypeError
Q4
B — for n in nums: if n % 2 == 0: return n
Q5
B — return is inside the loop, so it stops after the first item
Section B: Match the Pairs
Q1
def add(a, b): print(a + b) total = add(2, 3) → total becomes None, because add() prints the sum but never returns it
Q2
def add(a, b): return a + b total = add(2, 3) → total becomes 5, because return sends the sum back to the caller
Q3
A return placed inside the loop ends the function on the first pass, so only the first item is handled → def total(nums): s = 0 for n in nums: s += n return s
Q4
A local variable only exists inside its function, so the caller must receive its value with return → def make_total(a, b): result = a + b return result
Q5
def area(w, h): print(w * h) big = area(4, 5) + 10 → area() prints instead of returning, so it gives back None and None + 10 causes a TypeError. Replace print with return.
Q6
def make_total(a, b): result = a + b make_total(2, 3) print(result) → result is a local variable, so it cannot be used outside the function and causes a NameError. Return it and store the answer from the call.
Section C: Definitions
Q1
return — Sends a value back to the caller. If a function only prints and has no return, it gives back None, so the caller cannot use the result.
Section D: Fill in the Blanks
Q1
def add(a, b): return a + b
Q2
def show(n): print(n) print(show(3) == None)
Q3
def first(nums): for n in nums: return n
Q4
def divide(a, b): return a / b print(divide(10, 2))
Q5
def area(w, h): return w * h print(area(3, 4))
Section E: Spot the Error
Q1
A — The parameters are in the wrong order. The call take_away(10, 4) puts 10 into small and 4 into big, so the function returns -6 instead of 6. The header should be def take_away(big, small):.
Q2
B — The function prints the answer instead of returning it, so x is None and x + 1 causes a TypeError. The line should be return n * 2.
Q3
D — print only shows the value on screen. The function then gives back None, so score + 1 fails. It should be return avg.
Q4
C — The return keyword is missing. The function works out the value but never sends it back, so volume is None and volume + 1 fails. The line should be return result.
Q5
B — The result is stored in a local variable new_mark, which disappears when the function ends. With no return, the caller gets None. The line should be return mark + 5.