Students give a group of steps a name with def, then use that name again and again, and see why one change inside a function updates every use.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Predict and Run 10 min · Investigate 15 min · Modify 15 min · Make 10 min
Read the unit primer "Functions in 5 minutes" first. Then run the def version and write the triangle function yourself. The surprise for students: def square(): on its own draws nothing. It only describes the steps; square() runs them.
A def on its own does nothing until it's called. When nothing draws, ask where the function is called.
| What you will see | A question to ask | The fix |
|---|---|---|
| Nothing is drawn, and there is no error. | Where in your code do you call the function, with brackets? | A def only describes. Add a line square() (not indented) below the def. |
| An IndentationError, or only part of the shape is in the function. | Which lines are pushed in under the def line? | Every line that belongs to the function must be indented under def. |
| Two functions with the same name; only the second one ever runs. | How many functions are called square? | Give each function its own name. A second def with the same name replaces the first. |
def square():
for i in range(4):
forward(100)
right(90)
square()
One possible solution for each task. Other correct programs are fine.
from turtle import *
def square():
for i in range(4):
forward(50)
right(90)
square()
from turtle import *
def square():
for i in range(4):
forward(100)
right(90)
def triangle():
for i in range(3):
forward(100)
right(120)
square()
triangle()
from turtle import *
def square():
for i in range(4):
forward(100)
right(90)
def triangle():
for i in range(3):
forward(100)
right(120)
def gap():
penup()
forward(130)
pendown()
square()
gap()
triangle()
gap()
square()
from turtle import *
def hexagon():
for i in range(6):
forward(40)
right(60)
def gap():
penup()
forward(100)
pendown()
penup()
goto(-180, 0)
pendown()
hexagon()
gap()
hexagon()
gap()
hexagon()
from turtle import *
def wall():
for i in range(4):
forward(100)
right(90)
def roof():
left(60)
forward(100)
right(120)
forward(100)
right(120)
forward(100)
right(180)
def door():
penup()
forward(40)
right(90)
forward(60)
left(90)
pendown()
for i in range(2):
forward(20)
right(90)
forward(40)
right(90)
wall()
roof()
door()
Stretch: Write a function star() (five lines, turning 144 degrees each time) and use it to draw a row of stars.
Support: Give the def square() code and ask students only to add a triangle() below it by copying and changing two numbers.
A program has def square(): with a loop inside, and nothing else. What does it draw, and why?
Nothing. The def only describes the steps; the function has to be called with square() to run them.
Next lesson gives functions inputs (parameters), so one function can draw many sizes. Every app is built from named functions. Class ideas: each student writes one shape function and the class combines them into a picture; time how long it takes to change three copies versus one function.