Give a group of commands a name, then use that name again and again.
Every app is built from small named functions that other code calls. You are about to write your first.
You already know how to repeat steps with a loop. Now you will give a whole shape a name.
Make your prediction first. Help opens once you've locked it in.
from turtle import *
for i in range(4):
forward(100)
right(90)
penup()
forward(150)
pendown()
for i in range(4):
forward(100)
right(90)
penup()
forward(150)
pendown()
for i in range(4):
forward(100)
right(90)
This program draws three squares by repeating the same code. What does it draw? Count how many times the number 100 appears.
Run it. Then make all three squares half as big. Notice how many places you had to change.
from turtle import *
for i in range(4):
forward(100)
right(90)
penup()
forward(150)
pendown()
for i in range(4):
forward(100)
right(90)
penup()
forward(150)
pendown()
for i in range(4):
forward(100)
right(90)
def square(): gives the loop a name. The steps only run when the name is called: square().
from turtle import *
def square():
for i in range(4):
forward(100)
right(90)
square()
penup()
forward(150)
pendown()
square()
penup()
forward(150)
pendown()
square()
Compare this with the copy-and-paste version from the last step.
What does each part of this program do?
Build the function from scrambled lines.
This is one file.
from turtle import *
def square():
for i in range(4):
forward(100)
right(90)
def square():
for i in range(3):
forward(100)
right(120)
square()
To make all three squares half as big now, how many numbers change?
Match each part of the program with its job.
Put the lines in order so the program draws one square.
Change the size of the square inside def square(): and run it.
Change a number inside the def, not the call.
Write def triangle(): following the pattern of square.
Copy the pattern of square: the word def, a name, brackets, a colon, then the indented steps.
A triangle has 3 sides. The turn at each corner is not 90.
Draw a square, then a triangle, then a square again.
Call each function by its name with brackets, like square().
Write def triangle() and run your program.
Run your program. This ticks when your code does all of the above.
Call square twice and triangle at least once.
Run your program. This ticks when your code does all of the above.
Work this out on paper first.
def square():
for i in range(4):
forward(100)
right(90)
square()
square()
square()
The program above calls square three times. How many times does forward(100) run altogether?
Write a function that draws any shape you like (a rectangle, a hexagon, a star), then call it three times in a row.
Start with a shape you can already draw with a loop, then put def name(): above it.
Write your function and call it three times.
Run your program. This ticks when your code does all of the above.
A house plan is a few small named pieces: wall() and roof(). Run it. Then add a door() function and call it. Notice that you never edit wall or roof to do it.
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. About 50 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
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.
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.
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.