Repetition

Now suppose we want to have turtles draw hexagons, septagons, octagons, etc. Instead of modifying the pentagon procedure over and over again, we might try to create a single procedure with two input variables:

to polygon [side num-sides]
   ; ???
end

The problem here is that the turtle must move forward num-sides times. But we won't know the value of this variable until the procedure is executed.

We can solve this problem using the repeat command. Here's the syntax:

repeat num-times [commands]

Paste the following definition into the Procedures tab:

to polygon [side num-sides]
   let angle 360 / num-sides
   pd
   repeat num-sides [fd side rt angle]
end

Now ask the observer to execute the following commands:

ask turtle 0 [polygon 10 3]

ask turtle 4 [polygon 10 4]

ask turtle 6 [polygon 6 6]

ask turtle 9 [polygon 1 30]

Here's a screen shot of the result. Notice that turtle 9's polygon almost looks like a circle: