Lesson 5 · The Polygon Function
Lesson 5 · The Polygon Function
Teacher mode is on. Toggle it off (bottom-right) to preview the student view.
This is the big one. You'll combine everything from Lessons 2–4 — loops, functions, parameters, and the angle formula — into a single function that draws any regular polygon. Tell it how many sides and how long, and it figures out the rest.
Learning Objectives
By the end of this lesson you will be able to:
- Combine a loop, a function, and parameters to solve one bigger problem
- Compute the turn angle inside the function with
360 ÷ sides - Test one function against many shapes (triangle, square, hexagon, octagon…)
- Explain how one general function replaces a whole pile of specific ones
One function to draw them all
So far each shape needed its own function. But look at the pattern: every polygon
is repeat N times: drive a side, turn 360 ÷ N. The only things that change are
the number of sides and the side length — perfect for parameters. And the
angle? The function can calculate it itself.

The new trick is putting a math division block (360 ÷ sides) right into the
Turn block's angle slot. Now sides does double duty: it sets how many times the
loop repeats and it sets the angle.
Notice what is not a parameter: Effort stays at 0.5 in both blocks. A
parameter earns its place when callers actually need to vary it — sides and
side_length change with every shape, speed doesn't.
Activity · Test it like an engineer
Real programmers test one function against many inputs. In your main program, call
polygon with different values and predict each shape before it runs:
Wait for button press
call polygon (sides: 4, side_length: 30)
call polygon (sides: 3, side_length: 30)
call polygon (sides: 6, side_length: 20)
call polygon (sides: 8, side_length: 15)
Write down what you expect each line to draw before you upload. Then run it and see whether the robot agrees with you.
What turn angle will polygon compute for a 12-sided shape (a dodecagon)?
polygon (8, 25) traces an octagon about 2 m around. What does polygon (8, 50) do?
Activity · Explore
- Run the four test shapes above and check each one roughly closes back to its start.
- Notice what happens as sides go up (10, 12, 20): the shape gets closer and closer to a circle.
- Challenge: draw a pentagon,
Turn 72°, then draw the pentagon again — the two overlapping pentagons make a star/mandala.
Without a general polygon function, you'd write one function each for square, triangle, pentagon, hexagon, and octagon. With polygon, how many functions do you need?
Real-world connections
Writing one flexible tool instead of many rigid ones is the heart of good engineering:
Libraries
A charting library draws any chart from parameters — you don't write new code for every graph.
Parametric CAD
Engineers define a part once with parameters, then generate every size from the same model.
Configurable machines
One programmable machine makes many products by changing inputs, not by rebuilding the machine.
Wrap-up
- What two parameters does
polygontake, and what does each control? - How does the function get the turn angle? (
360 ÷ sides, computed inside.) - Why is one general function better than many specific ones? (Less code, fewer bugs, easier to change.)