← Teacher guide

Functions

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.

Python functions and events · Functions · about 50 minutes · Open the student lesson

Classroom resources

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.

Students will be able to

Timing

Predict and Run 10 min · Investigate 15 min · Modify 15 min · Make 10 min

Before class

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.

Running the lesson

  1. Predict and Run: the copy-and-paste program works, but making the squares smaller means changing three numbers. Let students feel that.
  2. Investigate: introduce def as 'teach Python a new word'. def square(): teaches it; square() uses it. Ask: what does the program draw if we delete the three square() lines?
  3. The match task separates describing (def) from doing (calling). Spend time here: it is the idea everything else builds on.
  4. Modify: students write triangle by copying the pattern of square. Check they call it as well as define it.
  5. Make: any shape is fine. The check is a def and three calls.

The hard part (on purpose)

A def on its own does nothing until it's called. When nothing draws, ask where the function is called.

When students get stuck

What you will seeA question to askThe 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.

Answers to the checks

  1. Count the changes: To make all three squares half as big now, how many numbers change?
    1
    Only the numbers inside def square(): change. The three calls are just the name square(), so they never need editing.
  2. Match the parts: Match each part of the program with its job.
    def square(): → C. gives the steps a name; square() → B. runs the steps; for i in range(4): → A. repeats the four sides; forward(100) → D. moves the turtle
    def only describes the steps. Calling the name is what runs them.
  3. Put it in order: Put the lines in order so the program draws one square.
    see the program below
    The def comes first, indented lines belong inside it, and the call comes after.
    def square():
        for i in range(4):
            forward(100)
            right(90)
    square()
    
  4. Same name twice: Someone pasted a second def square below the first. What does square() draw?
    A triangle
    A def gives a name to some steps. The second def gives the same name to new steps, so by the time square() is called it means the triangle. Python gives no warning: the first def is simply lost.
  5. Mix it up: The program above calls square three times. How many times does forward(100) run altogether?
    12
    Each call runs the loop 4 times, and the function is called 3 times, so forward(100) runs 4 × 3 = 12 times.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify › Resize the square

from turtle import *


def square():
    for i in range(4):
        forward(50)
        right(90)


square()

Modify › Write a triangle function

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()

Modify › Use both

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()

Make

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()

Extend the house plan

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 and support

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.

Exit ticket

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.

What each step is for

Where it leads

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.