4 Variables
Sometimes, we may want to draw shapes that are not exactly predetermined. For example, we may want to draw squares,
pentagons and other shapes. Of course, knowing how to draw a triangle, we only need to tweak the steps to draw other
shapes.
But that’s tedious.
Another way to solve this problem is to write the logic to draw an N-sided polygon using some rules. Assuming that we
want all sides of the polygon to have the same length, we just have to change the right turn angle and the number of times to
perform the steps.
We can use the word repeat to perform a sequence of actions some number of times. For example, to draw a triangle, we
could use the following code:
repeat 3 [forward 200 right 120]
Type this commend on the command bar, and you can see the shape drawn.
This is cool! However, to change this code to draw a pentagon or a square, both the loop count (3) and the turn angle
(120) needs to be changed.
We can use a variable n to indicate the number of sides. Let us store this method as follows (use the editor to type this).
You can append to the drawtriangle code that we edited earlier:
to drawpolygon :n
repeat :n [forward 200 right 360/:n]
end
Press the triangle tool to update and close the editor. Now, on the command bar, type drawpolygon 4 to draw a square,
or drawpolygon 5 to draw a pentagon.
Let us look more closely to the code and find out what it does:
- to drawpolygon :n
- to tell Logo that we are defining a sequence of steps that can be recalled later by its name. Because to is
a keyword, we must spell it as to.
- drawpolygon is the name of this sequence of steps. We can choose other names.
- :n tells Logo that this sequence of steps requires a number, and this number has a name of n in the
sequence. n is, essentially, a variable. This is because when we draw a triangle, n is 3, when we draw a
square, n is 4. The colon in front of n is necessary as we are referring to a variable named n.
- repeat :n [forward 200 right 360/:n]
- repeat is a special command that performs some steps a certain number of times. repeat is a keyword,
which means that it must be spell as such.
- :n specifies the number of times to repeat an action. It can be any expression that evaluates to an integer.
For example, 4 is perfectly fine if we want to specify to perform an action 4 times. However, in our case,
the number of times depends on the number of sides of the polygon, which is stored in variable n. Note
that the colon in front of n is required to refer to the value of variable n.
- [...] is a list of actions. The square brackets [ and ] are used to enclose a number of items to form a single item
called a list. In this example, the list is the action to perform n times. Since the list has multiple actions inside, all
of these actions are performed the same number of times. Let us take a closer look at the actions specified inside
the list:
- forward 200 tells the turtle to forward 200 pixels.
- right 360/:n tells the turtle to make a right turn. The number of degrees to turn is 360/:n, which
means 360 divided by the value of variable n. When we draw a square, n has a value of 4, and so the
command rotates 90 (360 divided by 4 is 90) degrees.
- end
- This simply marks the end of the method called drawpolygon.
When we use the method drawpolygon, we simply type the name of the method drawpolygon, followed by a number that
becomes the value of variable n. In other words, when we execute the command drawpolygon 6, Logo understands that we
need to use the method called drawpolygon. Furthermore, Logo also uses the expression immediately following (in this case,
6) to initialize variable n before steps in the method are carried out.