Make one function draw many different shapes, and keep your best ones as tools.
One well-made function can do the work of a hundred copied lines: sending a message, drawing a tile, converting a unit.
Last lesson square() always drew the same size. A parameter lets each call say what it needs.
Predict: Read the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Run: Run it and compare the result with your prediction.
Investigate: Trace how each line of code connects to what you see.
Modify: Change the code on purpose and check the result.
Make: Build something new using what you have learned. Expect a few tries; that is normal.
Make your prediction first. Help opens once you've locked it in.
PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Predict
params.py
from turtle import *
defsquare(size):
for i inrange(4):
forward(size)
right(90)
square(50)
square(100)
square(150)
size is a parameter. What will this draw? Where do the three squares share a corner, and why?
RunRun it and compare the result with your prediction.
Run
Run it and compare it with your prediction. Then call square with other sizes.
params.py
from turtle import *
defsquare(size):
for i inrange(4):
forward(size)
right(90)
square(50)
square(100)
square(150)
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.
Investigate
polygon(sides, size) has two parameters. The angle is calculated from sides, just like in the variables lesson.
params.py
from turtle import *
defpolygon(sides, size):
angle = 360 / sides
for i inrange(sides):
forward(size)
right(angle)
polygon(3, 80)
polygon(4, 80)
polygon(6, 80)
What does each call pass in, and what does the function work out?
Parameters and arguments sound alike. Match them up.
Fill in what each call passes to polygon(sides, size), and the angle it calculates.
call
sides
size
angle
polygon(3, 80)
3
80
polygon(4, 80)
4
80
polygon(6, 80)
Match each piece with its role.
sides in def polygon(sides, size):
6 in polygon(6, 80)
angle = 360 / sides
polygon(4, 80)
ModifyChange the code on purpose and check the result.
Modify
Call polygon with sides and size of your choice.
Need a hint?
Add a line like polygon(8, 50) at the bottom.
Change polygon so each call can choose its own pen colour, like polygon(3, 80, "red").
Need a hint?
A new parameter goes in the brackets of the def, and a new value goes in every call.
Another hint
Inside the function, pencolor(...) takes the value of your new parameter.
Try it yourselfYour edits are saved in this browser.
Add a fourth call to polygon and run it.
Run your program. This ticks when your code does all of the above.
Give polygon a third parameter for the pen colour.
Run your program. This ticks when your code does all of the above.
InvestigateTrace how each line of code connects to what you see.
Mix it up
Work this out on paper first.
defpolygon(sides, size):
for i inrange(sides):
forward(size)
right(360 / sides)
What is the turn in degrees for polygon(8, 50)?
MakeBuild something new using what you have learned. Expect a few tries; that is normal.
Make
Write def pattern(sides, copies): that draws copies polygons, turning a little between them so they fan out around a point.
Try it yourselfYour edits are saved in this browser.
Need a hint?
Use a loop to repeat. Inside it, call polygon, then turn a little.
Another hint
To go round once in total, each turn is 360 divided by the number of copies.
Write pattern(sides, copies) and run it.
Run your program. This ticks when your code does all of the above.
MakeBuild something new using what you have learned. Expect a few tries; that is normal.
Keep it as a button
You have unlocked Save selection to My tools. Open Reference, put the cursor inside one of your functions, and click it. It appears under My tools, ready to copy into any program. A saved tool is the first layer of abstraction: you can use the idea again without rewriting it.
Try it yourselfYour edits are saved in this browser.
ModifyChange the code on purpose and check the result.
Rearrange a floor plan
A floor plan is room(width, depth) called many times. Run it. Move the kitchen, add a bathroom, then change scale and see the whole plan resize.
Try it yourselfYour edits are saved in this browser.
For teachers
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. About 55 minutes.
I can write a function with parameters and call it with different values.
I can explain the difference between a parameter (the name in the def) and an argument (the value in the call).
I can call one function from inside another.
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
Predict: ask why the three squares share a corner. They all start where the turtle is, facing the same way; only size changes.
Run: students try their own sizes. Ask what would happen with square(-50).
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.
Modify: adding a colour parameter means changing the def and every call. That is the 'contract' between a function and its callers.
Make: pattern calls polygon in a loop and turns a little each time. Set speed(0) so it draws quickly.
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
A TypeError: polygon() missing 1 required positional argument. Ask: How many parameters does the def have, and how many values does this call pass? Fix: Every call must pass one value for each parameter, in the same order.
The shape has far too many sides or does not close. Ask: Which value went into sides, and which into size? Fix: Arguments match parameters by position. polygon(6, 80), not polygon(80, 6).
The pattern draws one shape on top of itself. Ask: Does the turtle turn between the copies? Fix: Add right(360 / copies) inside the loop, after drawing each polygon.
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.
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.
What each step is for
Investigate: A parameter lets one function make many results: each call passes arguments in order, and the function calculates from them.
Modify: Extending a function means changing its definition and every call; it shows why parameters are a contract between the def and its callers.
Mix it up: A parameter can be used in a calculation, like any name.
Now you can
Write a function with parameters and call it with different values.
Explain the difference between a parameter (the name in the def) and an argument (the value in the call).