Handout 2B

The purpose of this exercise is to gain more experience with LOGO procedures.

You know how to define simple LOGO procedures to draw simple pictures on the screen. By providing a little more information to the LOGO procedure you can draw more complicated pictures on the screen and ensure that your procedures will be useful for multiple applications.

There are two parts to the command FORWARD 30; one that tells the turtle which way to move and one that tells it how far. The part that tells it how far (30) is called an argument to the FORWARD procedure. Using arguments (usually) makes procedures more powerful and flexible; the FORWARD command would not be as useful if it could only move the turtle a fixed distance each time it was used.

When you write a procedure that uses an argument, you don't know what value that argument will take. You must use a variable quantity to refer to the value of that argument in the procedure definition. Names of variables in LOGO are preceded by :'s to distinguish them from names of commands.

Here is how to define a procedure to draw squares of different sizes:

TO SQUARE :SIDELENGTH
 REPEAT 4 [FORWARD :SIDELENGTH RIGHT 90]
END
Type in the definition of the SQUARE procedure, then try it out using the command SQUARE 50. Now try SQUARE 20. The argument really does change the size of the square generated by the procedure, and the two squares together look kind of nice. Use the new SQUARE command to make some nice patterns using different sized squares.


Of course, we can combine our new command with old commands we already know. The following sequence of commands draws a house!

SQUARE 50
FD 50
RT 30
FD 50
RT 120
FD 50
You can even create a HOUSE command with an argument that determines the size of the house drawn. When you do this, you are defining one new command in terms of another; try it now! Decorate your house if you like.

If you draw several houses of different sizes, you get a village. You could even write a VILLAGE procedure, then COUNTY, then STATE and so on, but first you should try putting together a small village "by hand".

As you build your village, you may find yourself moving the turtle from the top right corner of an old building to where you want the lower left corner of a new building to be. This is awkward, especially when writing a VILLAGE procedure and trying to guess how you want to move the turtle in order to start the next building.

The difficulty lies in figuring out where the turtle is after completing the HOUSE procedure. There's a simple way to remove this difficulty -- make sure that the turtle is back in the same position it started from when it finishes drawing the house!

The position and heading of the turtle is called its state. A procedure that leaves the turtle in the same state it started in is called state transparent.

Before defining a VILLAGE command we would like to have a state transparent HOUSE command. One way to do this would be to edit the HOUSE command and change it to be state transparent. A simpler way might be to define a different command that draws a house and is state transparent, like the one shown below:

TO STHOUSE :SIZE
 SQUARE :SIZE                             ; Draw the base of the house
 FD :SIZE RT 30 FD :SIZE RT 120 FD :SIZE  ; Draw the roof of the house
 RT 30 FD :SIZE RT 90 FD :SIZE RT 90      ; Return to starting position
END
This procedure definition also demonstrates two other features of LOGO. Comments have been added at the end of each line; anything after a ; character is ignored by LOGO. Use comments to remind yourself what each piece of your program does. Also, each line of the program has been indented a space. This does not make a big difference in a three line program, but for more complicated programs indentation can be very helpful.

If you have time, define a state transparent VILLAGE command using the STHOUSE command (or a personalized version of that command.)


If you quit LOGO and start it again, it will have forgotten all the new commands you defined. You probably don't want to have to type in all the commands we just went through each time you start LOGO. You don't have to; you can save your programs to a floppy disk.

For now we use the command SAVE to do this. SAVE saves all the commands you just defined to the file named as its argument. To save to a file named village on floppy disk named Untitled on a Macintosh computer, type:

SAVE "untitled:village
To load these definitions in to LOGO the next time you start the program, put the floppy in the drive and type:
LOAD "untitled:village
On a Windows computers you would use use the SAVE and LOAD commands, identifying the file location with a name like A:\village.


Mtht420