Table of Contents | Previous Page | Next Page
Copyright Don Sleeth, 1998 ©

Page 8 - Improving Square

Load in your work

From the file menu, open your project named Page6. Then from the file menu select "Save Project As..." and save your project as Page8. The last page, page 7 was just "throw-away code".

In the procedures page, your program should look like this:

	
====================
to Main
    cc
    talkto "t1
    setsh [Horse1 Horse2 Horse3]
    repeat 20 [Square]
end
====================

to Square
    forward 50 right 90 wait 1
    forward 50 right 90 wait 1
    forward 50 right 90 wait 1
    forward 50 right 90 wait 1
end

to Start
    Main
end
	

Adding an Input to our procedure

Way back at the end of Page 6 I suggested that it would be nice if we could decide how big of a square our turtle should make. Then we would have a nice, general procedure that could be used anytime we want a square of any size. You won't believe how easy it is to make this improvement, now that we understand variables.

We want to turn our procedure Square, which currently has no inputs and no outputs, into a procedure that receives one input. We do that simply by thinking of a good name for the input and putting it on the line that declares the procedure. Make the following change to Square

	
to Square Length
    forward 50 right 90 wait 1
    forward 50 right 90 wait 1
    forward 50 right 90 wait 1
    forward 50 right 90 wait 1
end
	
Adding the name Length to the first line of the procedure, the declaration line, has the same effect as the instruction local "Length, it takes a spot in memory and names it Length so we can store a value in it. The neat thing is that we don't have to use make to put the value in, the value comes from the input to the procedure.

If you were to go back to the program page and try to run your program now, what would happen? Think about it before you do it and then give it a try. Were you right? Well, if you guessed an error message you were. This is because we have now changed what we taught MicroWorlds about the word Square. We have now taught it that Square requires one input.

To fix this error we need to change Main so that when it calls Square, it satisfies Square's new need for an input. Let's give it the number 30.

	
to Main
    cc
    talkto "t1
    setsh [Horse1 Horse2 Horse3]
    repeat 20
         [Square 30]
end
	

Now you can try your program and you will find that it works, but it really isn't any better than before. Our revised procedure Square requires an input, and gets an input, but it doesn't actually use it for anything. Before we fix that, let's tidy up Square. It has four lines of instruction and they are all the same. We can do better than that.

	
to Square Length
    repeat 4
        [forward 50 right 90 wait 1]
end
	
We have improved the "readability" by using the repeat procedure which has 2 inputs, a number and a list of instructions. Of course it wouldn't hurt to Ctrl-F back to your program and run it just to be sure everything works.

Now let's fix Square so that it makes use of the variable Length. When you call the procedure from Main by saying Square 30 Logo stores the value in the input variable Length. We want the value of Length to be used as the input to forward. To get the value of a variable, we use the procedure thing. Change your Square procedure as follows:

	
to Square Length
    repeat 4
       [forward thing "Length right 90 wait 1]
end
	

Now, to find the input for forward, Logo must run thing which has, as its output, the value stored in Length. The value stored there is the input when the procedure Square is called by Main. Play with your new, more powerful Square by changing the value it is called with (officially this value is called an "argument", I don't know why .. I'm not mad, are you mad? ;-), inside the procedure the variable is called a parameter.)

You can also call Square directly from the Command Center, for example by typing Square 200 and pressing Enter.

We have now finished a very good procedure that we can use any time we want. Here is my finished program:

	
to Main
    cc
    talkto "t1
    setsh [Horse1 Horse2 Horse3]
    repeat 20
        [Square 30]
end

to Square Length
    repeat 4
       [forward thing "Length right 90 wait 1]
end

to Start
    Main
end
	

Save your Page8 project. One thing that I have found handy is to have a file of completed procedures. I call that project file Library. We will also save the little Start and Main procedures in the Library so that if we want to start a new project some time, we just have to copy and paste them from the Library. Look over your procedures page and make sure it is free of errors and nice and neat, then go File Save Project As and save it as a file called Library. Now, so that you don't inadvertently mess up your Library, go File Open Project and load Page8 back in.

Where to from here

Continuing on the theme of making procedures for use in animation, I would like to develop a procedure called Walk.

Table of Contents | Previous Page | Next Page