← Teacher guide

Functions with parameters

Students give a function inputs (parameters), so one function can draw many sizes and shapes, then build a pattern by calling one function from another.

Python functions and events · Functions · about 55 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 · My tools 5 min

Before class

Run the three nested squares and try adding a colour parameter to polygon. Two words to have straight: a parameter is the name in the def (size), an argument is the value you pass when calling (50). The argument fills the parameter, in order.

Running the lesson

  1. Predict: ask why the three squares share a corner. They all start where the turtle is, facing the same way; only size changes.
  2. Run: students try their own sizes. Ask what would happen with square(-50).
  3. Investigate: arguments go to parameters in order. polygon(6, 80) fills sides with 6 and size with 80. Swapping them gives an 80-sided shape.
  4. Modify: adding a colour parameter means changing the def and every call. That is the 'contract' between a function and its callers.
  5. Make: pattern calls polygon in a loop and turns a little each time. Set speed(0) so it draws quickly.
  6. My tools: students save a function to My tools in the Reference panel. It is a copy they can paste into later programs.

The hard part (on purpose)

Arguments fill parameters in order. Read the call and the def aloud side by side and match each value to its name.

When students get stuck

What you will seeA question to askThe fix
A TypeError: polygon() missing 1 required positional argument.How many parameters does the def have, and how many values does this call pass?Every call must pass one value for each parameter, in the same order.
The shape has far too many sides or does not close.Which value went into sides, and which into size?Arguments match parameters by position. polygon(6, 80), not polygon(80, 6).
The pattern draws one shape on top of itself.Does the turtle turn between the copies?Add right(360 / copies) inside the loop, after drawing each polygon.

Answers to the checks

  1. Trace three calls: Fill in what each call passes to polygon(sides, size), and the angle it calculates.
    120, 90, 6, 80, 60
    Arguments go to the parameters in order, and angle = 360 / sides inside the function.
  2. Name the parts: Match each piece with its role.
    sides in def polygon(sides, size): → C. a parameter: filled in by each call; 6 in polygon(6, 80) → D. an argument: the value for sides; angle = 360 / sides → B. calculated inside the function; polygon(4, 80) → A. a call: runs the function with these values
    Parameters are the names in the def. Arguments are the values you pass when you call it.
  3. Mix it up: What is the turn in degrees for polygon(8, 50)?
    45
    The turn is 360 / sides, and sides is 8, so 360 / 8 = 45. The size does not change the turn.

Solutions

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

Modify › Draw one more shape

from turtle import *


def polygon(sides, size):
    angle = 360 / sides
    for i in range(sides):
        forward(size)
        right(angle)


polygon(3, 80)
polygon(4, 80)
polygon(6, 80)
polygon(8, 50)

Modify › Add a colour parameter

from turtle import *


def polygon(sides, size, colour):
    pencolor(colour)
    angle = 360 / sides
    for i in range(sides):
        forward(size)
        right(angle)


polygon(3, 80, "red")
polygon(4, 80, "blue")
polygon(6, 80, "green")
polygon(8, 50, "purple")

Make

from turtle import *


def polygon(sides, size):
    angle = 360 / sides
    for i in range(sides):
        forward(size)
        right(angle)


def pattern(sides, copies):
    for i in range(copies):
        polygon(sides, 60)
        right(360 / copies)


speed(0)
pattern(5, 12)

Keep it as a button

Open task: any program that does what the step asks.

Rearrange a floor plan

from turtle import *

scale = 15  # pixels per metre


def room(width, depth):
    for i in range(2):
        forward(width * scale)
        right(90)
        forward(depth * scale)
        right(90)


def move_to(x, y):
    penup()
    goto(x * scale, y * scale)
    pendown()


move_to(-8, 4)
room(5, 4)    # bedroom
move_to(1, 4)
room(4, 4)    # kitchen, moved to the right
move_to(-3, 4)
room(4, 2)    # bathroom
move_to(-8, 0)
room(9, 3)    # living room

Stretch and support

Stretch: Add a third parameter to pattern for the size, and draw a gallery of three different patterns side by side.

Support: Give students a table of calls (square(50), square(100)) and ask them to sketch what each draws before running.

Exit ticket

def polygon(sides, size): ... What does polygon(5, 40) draw?

A pentagon (5 sides) with each side 40 steps long, turning 72 degrees at each corner.

What each step is for

Where it leads

Next lesson functions give answers back with return. Parameters are how every app passes information around. Class ideas: a pattern gallery where each student chooses sides and copies; compare function designs and talk about what makes one easy to reuse.