Using pockets

Page 14

When you want the turtle to go forward 100 you write:

FD 100

If you just tell him FD he won't know what to do. He knows he should walk forward. But how far? You must tell him before he can start walking. That's why words like FD and RT need numbers. Other words don't need numbers. For example CS. CS means clear the screen and that's it.

The new words we have taught the turtle work like CS. SQUARE is always a square of the same size. You can't say SQUARE 10. The turtle won't understand what to do with 10. But wouldn't it be great if he did? Then squares could be different sizes. We could say SQUARE 10 and he would draw a small square. Or SQUARE 100 and he would draw a big square. This is the old code for SQUARE:

TO SQUARE
  REPEAT 4 [FD 100 RT 90]
END

Now let's change it. We want the square word to use a parameter. Just like FD when you say FD 100. A parameter is something you must tell the turtle every time you use the word. Think of it like this: the turtle has a couple of pockets. Every pocket has a word written on it. When we want him to remember a number we tell him which pocket to put it in. Then he can look in that pocket whenever he needs it.

Does that sound confusing? I bet. But it's really not difficult at all. Let's look at an example. This is SQUARE with a parameter:

TO SQUARE :top
  REPEAT 4 [FD :top RT 90]
END

It's almost the normal SQUARE. There are two differences. On the first line we tell the turtle that he should put what we give him in the pocket called :top. Then whenever :top shows up again he can look in the pocket. So if we write SQUARE 50 the turtle will write 50 on a slip of paper and put it in his :top pocket. When he sees FD :top he will look in his :top pocket. Then he will see 50. So he will go FD 50.

Let's change another old word. Here is STAR with a parameter:

TO STAR :top
  REPEAT 12 [FD :top BK :top RT 30]
END

Here the turtle needs to look in the pocket twice. First to see how far he should walk FD. Then to see how far he should walk BK. Now we can write STAR 100. Try it! (Of course, first you must open the editor and write the new Logo code for STAR.)

Two stars

A small star (STAR 20) and a big star (STAR 100)

Here's another way we can use a parameter in STAR:

TO STAR :top
  REPEAT :top [FD 100 BK 100 RT 360/:top]
END

Try it out! What does STAR 10 draw? What does STAR 100 draw? Do you understand why? (It's tricky! Don't worry if you can't figure it out.)

We can also have several parameters. We just put them in different pockets. Like this:

TO STAR :top :bottom
  REPEAT :top [FD :bottom BK :bottom RT 360/:top]
END

You don't have to give your parameters names with lowercase letters. I just like to do that because it looks nice. We can give these pockets any names we like. We know that :top is the number of spikes on the star and :bottom is the length of each spike. Isn't it more sensible to call the pockets by what they do? Let's call them :number and :length. Like this:

TO STAR :number :length
  REPEAT :number [FD :length BK :length RT 360/:number]
END

You can use any name. But it's sensible to avoid long names or names that are hard to spell!

Now you can make your own drawings with parameters. Try changing some of your old ones. Maybe you can draw a house with a parameter? Or a boat? Or something else. Or you could just play around. You don't have to know what you want to draw before you try.

These are the words we know now:

Logo word What it means
FD 100 Walk forward 100 steps
BK 100 Walk backward 100 steps
LT 90 Turn left (good for drawing squares)
RT 90 Turn right (good for drawing squares)
LT 120 Turn left (good for drawing triangles)
RT 120 Turn right (good for drawing triangles)
CS Remove everything you have drawn
HOME Move back to the starting point (and draw a line)
PU Lift the pen so you won't draw when you walk
PD Put the pen towards the screen again
REPEAT 10 [       ] Repeat what's in the box 10 times
SETPC 4 Change the pen to pen number 4
TO ... END Remember the word after TO in the future
; Don't listen to me now
PRINT [I'm a turtle!] Say "I'm a turtle!"
RANDOM 10 Choose a number from 0 to 9
WAIT 60 Wait for 1 second.
PRINT 2+2 Makes the turtle say 4
TO SQUARE :top Put 5 in your :top pocket when I say SQUARE 5

These are the words we have taught the turtle. They are the same as on the last page. But we have changed them. Now they have parameters:

New Logo word What it means
SQUARE :top Draw a square
CIRCLE Draw a circle
STAR :number :length Draw a star

(Why doesn't CIRCLE have a parameter too? Can you change that?)

Turn page, or back to the table of contents.