Lesson 4 of 5 · Gardens by the Bay
Functions: a 45-minute coding lesson
A free 45-minute lesson on functions for ages 8–12. Students define plant_tree once and reuse it across Gardens by the Bay.
- 45 minutes
- Pairs, whole class
- One device per pair, plus a projector
Learning objectives
By the end of the lesson, students can:
- Explain that a function is a named set of instructions.
- Define a function and call it several times.
- Combine functions with loops to shorten a program.
Key words
- Function:
- A set of instructions with a name, which you can use again and again.
- Define:
- Teach the computer what a function name means.
- Call:
- Use a function by its name.
You'll need
- One laptop, Chromebook or iPad per pair, open to dotcodecampus.com/maze/gardens/1. No accounts or installs.
- A projector or screen for the demo.
- Optional: the printable unplugged grid game for the warm-up, and certificates to finish.
Lesson flow
Minutes 0–8
Unplugged warm-up
- Teach the class a three-move dance and give it a name, like "the Merlion".
- Call out: "Merlion, clap, Merlion, jump, Merlion". Students perform it.
- Ask: how much shorter is that than calling out every move each time? Introduce the word function.
Minutes 8–15
Demo on the projector
- Open Gardens by the Bay level 1 (dotcodecampus.com/maze/gardens/1) and plant one tree with dig, plant, water.
- Move to level 2. Fill the "define plant_tree" block, then use plant_tree three times.
- Show the Python: "def plant_tree():" and point out how each tree is now one line.
Minutes 15–35
Paired play: levels 1–6
- Pairs work through levels 1–6, swapping roles at each level.
- Remind pairs that blocks inside "define" do nothing until the function is called.
- Level 4 has a function with loops inside it. Suggest writing the row first and testing it.
- Fast finishers can go for three stars everywhere to unlock "Supertree Grove".
Minutes 35–42
Class discussion
- What changed when you moved dig, plant, water into a function?
- If planting needed a fourth step, how many places would you change, with and without a function?
- What everyday routines could be functions? (Make breakfast, pack a school bag.)
Minutes 42–45
Exit ticket
- Define a function called make_kopi with three steps, then write a program that makes kopi for two people.
Early finishers: Design a garden that needs a function called plant_row, and solve it with a loop inside the function.
Answer key
One three-star solution per level, in Python. Other solutions with the same number of blocks earn three stars too. Jump links take the class straight to a level.
Level 1: First Seed
4 blocksdotcodecampus.com/maze/gardens/1
move_right() dig() plant() water()
Level 2: Teach plant_tree
11 blocksdotcodecampus.com/maze/gardens/2
def plant_tree():
dig()
plant()
water()
move_right()
plant_tree()
move_right()
move_right()
plant_tree()
move_right()
move_right()
plant_tree()Level 3: Functions in Loops
7 blocksdotcodecampus.com/maze/gardens/3
def plant_tree():
dig()
plant()
water()
for i in range(5):
move_right()
plant_tree()
move_right()Level 4: Row by Row
10 blocksdotcodecampus.com/maze/gardens/4
def plant_row():
for i in range(3):
move_right()
dig()
plant()
water()
for i in range(3):
move_left()
for i in range(3):
plant_row()
move_down()Level 5: Supertree Steps
7 blocksdotcodecampus.com/maze/gardens/5
def plant_tree():
dig()
plant()
water()
for i in range(4):
move_right()
plant_tree()
move_down()Level 6: Flower Dome Path
13 blocksdotcodecampus.com/maze/gardens/6
def plant_tree():
dig()
plant()
water()
for i in range(3):
move_right()
plant_tree()
move_right()
move_down()
move_down()
for i in range(3):
move_left()
plant_tree()
move_left()Bonus: Supertree Grove
12 blocksdotcodecampus.com/maze/gardens/bonus
def plant_tree():
dig()
plant()
water()
for i in range(2):
for j in range(4):
move_right()
plant_tree()
move_down()
for j in range(4):
move_left()
plant_tree()
move_down()