0% found this document useful (0 votes)
136 views10 pages

Chapter 4 em

The document introduces how to define a procedure called SQUARE in Logo to teach the turtle to draw a square. It explains using the Logo editor to define procedures which allows for easy fixing of typing errors. The editor is demonstrated to define SQUARE and another procedure called SQUARESTAR. Various uses of the new SQUARE procedure are shown such as using it to draw a cross, flags, and many flags designs.

Uploaded by

Cynthia Solomon
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views10 pages

Chapter 4 em

The document introduces how to define a procedure called SQUARE in Logo to teach the turtle to draw a square. It explains using the Logo editor to define procedures which allows for easy fixing of typing errors. The editor is demonstrated to define SQUARE and another procedure called SQUARESTAR. Various uses of the new SQUARE procedure are shown such as using it to draw a cross, flags, and many flags designs.

Uploaded by

Cynthia Solomon
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

A First Procedure: Teaching the Turtle to Draw a Square

Chapter 4

A First Procedure: Teaching the Turtle to Draw a Square


Using the commands f o r w a r d and r i g h t or l e f t we can make the turtle draw a square. For convenience we will often use the short names for commands.

FD RT FD RT

30 90 30 90

FD RT FD RT

30 90 30 90

If we used 50 instead of 30, the turtle would draw a bigger square. We could use any number. Lets define a new command in Logo to get the turtle to draw a square. New commands in Logo are programs or procedures written by you. Each time you want a square you can use your new pro cedure rather than retyping the individual in structions. To define a new command you should first choose a name. Lets use s q u a r e because that seems natural. But any name will do. You could then use the Logo command t o and define s q u a r e in the same way as you did g r e e t . That is, you could type t o s q u a r e , then the in structions, then e n d . This method is good if you

SQ UA RE

are careful about typing and you know just what you want to type in advance. Introducing the Logo Editor There is another way to define a procedure. You could use the Logo editor. Then, if you make typ ing mistakes you can remove them easily. When you are using the editor, Logo carries out only ed iting actions. There is a disadvantage to using the editor. Your turtle drawings will be replaced by the editing screen and the picture will be lost. On the other hand, the disadvantage of using t o to define your new procedure is that you will not be able to fix typing errors except on the line where the I (cursor) rests.
EDIT e d it

or e d signals Logo that you want to edit. Follow it by the name of the procedure you want to edit. You must prefix the name with a " (quote mark). Do not type a space between " and the name of the procedure.
EDIT "SQUARE
Remember the
"

(quote mark).

After you press the r e t u r n key you will be using the Logo editor and only editing actions will be carried out. Bug Box If you do not remember to prefix a " (quote mark) and type
EDIT SQUARE

sq u a re

with

Logo will respond


I DON'T KNOW HOW TO SQUARE

If you had turtle drawings on the screen they will go away when you start editing. When the editor starts up, the title line of the pro cedure will appear at the top of the screen.
TO S Q U A R E
This is the title line.
to

LOGO FfllTflR

informs Logo that the following text is part of a procedure definition.

s q u a r e is the name of the procedure. You are free to choose another name.

T he! (cursor) is at the end of the title line. Notice that Logo does not print ? or any other prompt symbol while you are using the editor. Since you do not want to change the title line press the r e t u r n key. Now type in the commands that make up s q u a r e . They are the commands you used previously.
FD RT FD RT FD RT FD RT 30 90 30 90 30 90 30 90

Think of everything you type in the editor as a stream of characters. If you want to add some thing to the stream, move the I (cursor) to that place.
29

If you want to move the I (cursor) backwards in the stream, use c t r l - b . Y o u can move all the way to the beginning of the stream. If you want to move the I (cursor) forward, use
CTRL-F.

Then type the characters you want and they will become part of the stream. Thus, if you have made a typing error in a pre vious line, use c t r l - b to move the I (cursor) back wards to where the bug is. When the I (cursor) p asses over the characters they remain un changed. If you want to erase a character the - key will erase the one to the left of the I (cursor) and will move the I (cursor) to that position. Notice if th e! (cursor) is at the beginning of a text line, pressing the - key will move it along with the entire line of text to the end of the previous text line. Thus
F ORWARD 3 0 RIGHT 90

becomes
FORWARD 3 0 R I G H T 90

Press the r e

tu rn

key to separate the lines again.

FORWARD 3 0 RIGHT 90

If you had pressed space instead of the key then you would see
FORWARD 3 0 RIGHT 90

re tu rn

30

You can move the I (cursor) from the last charac ter typed to the t in t o on the title line by typing c t r l - b several times. Type c t r l - c when you have completed your edit ing. If you do not type e n d , Logo will insert the word when you type c t r l - c . Logo will now end the procedure definition and carry out the t o command by defining s q u a r e . Logo will type
SQUARE DEFINED

Using the New Command Try your new command. Type


SQUARE

Again, type
SQUARE

This time the turtle just retraced its path. If you turn the turtle left or right and then type s q u a r e again, a new drawing will appear. For ex ample, tell the turtle
SQ U A RE

RIGHT

45

and now type


SQUARE

R E P E A T 3 [RT 45 SQ U A RE]

Continue to repeat these two commands ( r t 45 and s q u a r e ) . T o do this you could use the Logo command, r e p e a t , r e p e a t requires two inputs. For example,
REPEAT 3 [RT 45 SQUARE]

R E P E A T 3 [RT 45 SQ U A RE]

31

The first input indicates how many times to re peat the enveloped instructions. The second input is a list of instructions. The instructions must be enclosed in brackets. Think of the brackets as making an envelope. Complete the design by typing
REPEAT 3 CRT 45 SQUARE]

Now
EDIT

l e t s m a k e a p r o c e d u r e fo r t h i s d e s ig n a n d

Call i t SQUARE STAR


"SQUARESTAR

You will now be using the editor and the title line will be displayed on the screen.
TO SQUARESTAR

Notice that th e! (cursor) is at the end of the title line. Since you do not want to change the title line press the r e t u r n key and type
REPEAT END 8 [SQUARE RT 45]

Dont forget, you type ished editing.

c t r l

-c

when you are fin

It is always a good idea to try out your new proce dure. Put the turtle in its startup state in the cen ter of the screen facing straight up. Remember that CS will do this. Type
CS

and then
SQUARESTAR

32

Bug Box If your squares look like rectangles the bug is in your TV, and not in Logo. The Logo command s e t s c r u n c h allows you to change the aspect ra tio of the screen. If you want to know what the current setting is type
PR S C R U NC H

Logo prints a number which is the current set ting. Now try
SETSCRUNCH .8

Then type
CS SQUARE

If your square looks worse try


SETSCRUNCH 1

Try other settings until you are satisfied. Other Uses o f s q u a r e Once you have defined a procedure, you can use it as you would any Logo primitive such as f d , b k , l t , r t , etc. Thus, a procedure you define can be used as part of the definition of other procedures. This is one of the powerful features of Logo. For exam ple, there are many designs which use s q u a r e . Some more examples are:

c u
FLAG CROSS FLAGS MANYFLAGS

Both f l a g and f l a g b a c k make the turtle draw the same design, but they leave the turtle in dif ferent states. Both procedures leave the turtle with the same heading as they found it, but f l a g leaves the turtle in a different position from the one it started in.
fla g b a c k ,

on the other hand, leaves the turtle in the same position on the screen as it was found. We can see the effect of these differences in c r o s s and f l a g s , c r o s s runs f l a g four times while f l a g s runs f l a g b a c k four times.
TO F L A G FD 3 0 SQUARE END TO C R O S S REPEAT 4 END [FLAG RT 9 0 ]

FLA G

CROSS

TO F L A G B A C K FLAG BK 3 0 END TO F L A G S REPEAT 4 END [FLAGBACK RT 9 0 ]

FLA G B A C K

M A NYFLA GS U s e s FLA G S.
TO M A N Y F L A G S FLAGS RT 4 5 FLAGS END

FL A G S

M A N Y FL A G S

34

Logo Vocabulary Full Name


R E PEA T SETSCRUNCH SCRUNCH C LEA R SCREEN SHOW TURTLE FORWARD R IG H T BACK LEFT TO END E D IT P R IN T

Short Name

cs
ST FD RT BK LT

ED PR

Special Keys
CTRL-G CTRL-B CTRL-F R ETU RN CTRL-C

You might also like