← WA Computer Science ATAR Unit 1

Side effects and pure functions

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.

Understanding Side effects and pure functions in WA Computer Science ATAR Unit 1

What it does: The global keyword lets the function change score outside itself, which is a side effect. A pure function can be checked with one call and an expected value, with no setup or hidden state. append() mutates the original list. Using + builds a new list and returns it, leaving the original unchanged. It has no printing, globals or mutation, so the same input always gives the same output. The function mutates its input, so the caller's list changes even though a value is returned.

Common mistakes: 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. 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):'.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 100–314) and word search (Modules 100–470).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which function is pure?
def add(a, b): return a + bdef add(a, b): print(a + b)
Matches
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).
Applying
Meanings
Term
side effect
Meaning
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.
Practice
scores = [70, 85]
Testing
Gaps
(n * 2)
Check Answer
Spots
2 tax = price * 0.1
3 print(price + tax)

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Ostrich' badge — Full Sprint, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which function is pure?
A. def add(a, b): return a + b ✓
B. def add(a, b): print(a + b)
The first function only returns a result from its inputs. Printing is a side effect, so the second one is not pure.
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 ✓
Changing a global alters data outside the function, so it is a side effect even though the code is written inside 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) ✓
append() mutates the original list. Using + builds a new list and returns it, leaving the original unchanged.
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
A pure function depends only on its inputs and only returns a result. Printing is a side effect, even if the function also returns 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
With no side effects, a test just compares the returned value to the expected value. No other state needs to be set up or checked.

Match the Pairs

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
def double(n): return n * 2 → A pure function: it only uses its input and returns a result, changing nothing outside itself
A function that changes a global variable has a side effect → def add_point(): global score score += 1
A pure function is easier to test because the output depends only on its inputs → assert square(4) == 16
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).
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.

Key Term

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.

Fill in the Blanks

1. def show(n): print(n * 2)
Hint: Type the built-in function that sends output to the screen, which is a side effect.
2. def add(n): global total total += n
Hint: Type the keyword that lets a function change a variable outside itself, causing a side effect.
3. def push(items, x): items.append(x)
Hint: Type the list method that changes the passed-in list in place, causing a side effect.
4. def plus(items, x): return items + [x]
Hint: Type the operator that joins two lists into a new one, leaving the original list unchanged.
5. def double(n): return n * 2 assert double(4) == 8
Hint: Type the comparison operator that checks the pure function's result equals the expected value.

Spot the Error

Question 1
A def add_tax(price):
B tax = price * 0.1
C print(price + tax)
D print(add_tax(50))
Line 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.
Question 2
A score = 0
B def add_point():
C return score + 1
D score = add_point(score)
Line 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):'.
Question 3
A def add_item(items, x):
B new = items.copy()
C new.append(x)
D return items
Line D — The function builds a new list called 'new' but returns the original 'items', so the added item is lost. It should return new.
Question 4
A def sorted_copy(nums):
B result = nums
C result.sort()
D return result
Line 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.
Question 5
A total = 0
B def add_to(total, n):
C return total + n
D add_to(total, 5)
Line 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)'.

Student with a class code? Join your class →