Side effects and pure functions

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

Side effects and pure functions

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which function is pure?
A def add(a, b): return a + b
B def add(a, b): print(a + b)
2. A function changes a global variable called total. Is this a side effect?
A No, because the change is inside the function body
B Yes, it changes something outside the function
3. Which function has a side effect on the list passed in?
A def add_item(items): return items + [5]
B def add_item(items): items.append(5)
4. Which statement about pure functions is true?
A Same inputs always give the same output and nothing outside changes
B They can print messages as long as they also return a value
5. Why are pure functions easier to test?
A You only need to check the output for given inputs
B You must also check globals and printed text after each call

Match the Pairs

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

Left columnRight column
1def add_item(items): items.append(5) Aassert square(4) == 16
2def double(n): return n * 2 Bsort() changes the passed-in list, which is a side effect, so it is not pure. Use return sorted(nums) to leave the original alone.
3A function that changes a global variable has a side effect CThe function prints, which is a side effect, and returns nothing, so x = None + 1 causes a TypeError. It should return sum(nums).
4A pure function is easier to test because the output depends only on its inputs Ddef add_point(): global score score += 1
5def total(nums): print(sum(nums)) x = total([1, 2]) + 1 EA side effect: the function changes the list that was passed in, so the caller's list is altered
6def sorted_copy(nums): nums.sort() return nums FA pure function: it only uses its input and returns a result, changing nothing outside itself

Definitions

side effect

Fill in the Blanks

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

1.
def show(n): _____(n * 2)
2.
def add(n): ______ total total += n
3.
def push(items, x): items.______(x)
4.
def plus(items, x): return items _ [x]
5.
def double(n): return n * 2 assert double(4) __ 8

Spot the Error

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

Question 1
A def add_tax(price):
B tax = price * 0.1
C print(price + tax)
D print(add_tax(50))
Question 2
A score = 0
B def add_point():
C return score + 1
D score = add_point(score)
Question 3
A def add_item(items, x):
B new = items.copy()
C new.append(x)
D return items
Question 4
A def sorted_copy(nums):
B result = nums
C result.sort()
D return result
Question 5
A total = 0
B def add_to(total, n):
C return total + n
D add_to(total, 5)

Answer Key

Side effects and pure functions
Section A: Quick Questions
Q1
A — def add(a, b): return a + b
Q2
B — Yes, it changes something outside the function
Q3
B — def add_item(items): items.append(5)
Q4
A — Same inputs always give the same output and nothing outside changes
Q5
A — You only need to check the output for given inputs
Section B: Match the Pairs
Q1
def add_item(items): items.append(5) → A side effect: the function changes the list that was passed in, so the caller's list is altered
Q2
def double(n): return n * 2 → A pure function: it only uses its input and returns a result, changing nothing outside itself
Q3
A function that changes a global variable has a side effect → def add_point(): global score score += 1
Q4
A pure function is easier to test because the output depends only on its inputs → assert square(4) == 16
Q5
def total(nums): print(sum(nums)) x = total([1, 2]) + 1 → The function prints, which is a side effect, and returns nothing, so x = None + 1 causes a TypeError. It should return sum(nums).
Q6
def sorted_copy(nums): nums.sort() return nums → sort() changes the passed-in list, which is a side effect, so it is not pure. Use return sorted(nums) to leave the original alone.
Section C: Definitions
Q1
side effect — A change a function makes outside itself, like printing, editing a global or changing a passed-in list. A pure function has no side effects and only returns a result from its inputs.
Section D: Fill in the Blanks
Q1
def show(n): print(n * 2)
Q2
def add(n): global total total += n
Q3
def push(items, x): items.append(x)
Q4
def plus(items, x): return items + [x]
Q5
def double(n): return n * 2 assert double(4) == 8
Section E: Spot the Error
Q1
C — Printing is a side effect, and the function now returns None. A pure function should use 'return price + tax' so the caller gets the result.
Q2
B — A pure function gets its input through parameters. Without the 'score' parameter it depends on a global variable, and the call add_point(score) on line 4 would raise a TypeError. The header should be 'def add_point(score):'.
Q3
D — The function builds a new list called 'new' but returns the original 'items', so the added item is lost. It should return new.
Q4
B — This does not copy the list. 'result' is the same list as 'nums', so sort() changes the caller's list, which is a side effect. Use nums.copy() instead.
Q5
D — A pure function does not change anything outside itself. The returned value is thrown away, so total stays 0. Assign it with 'total = add_to(total, 5)'.