LOGO Procedures

LOGO Procedures (“TO” Command)

LOGO “TO” command is used to define LOGO Procedures. The turtle understands a lot of words (commands) (like FORWARD, BACK, RIGHT, LEFT).  You can teach the turtle new words (vocabulary) by defining Procedures using the “TO” command.

The ‘TO’ command is similar to what are called “Procedures” and “Subroutines” in other programming languages.

By using the “TO” command, you can define new vocabulary, like ‘CIRCLE’, ‘SQUARE’, ‘OCTAGON’ etc. Once you save these new words (vocabulary), the turtle will remember these words. Now, you can use these new words as part of your future programs.

The “TO” command teaches the turtle new words and increases its vocabulary. Once the turtle understands these words, then you would not even have to write even one line of command. You just write the word ‘square’ or ‘pentagon’ etc. and the turtle will draw that shape for you.

In LOGO, programming is done by adding new words to the existing vocabulary. New words are defined using words you already know. In the following program, we use the “TO” command to teach the turtle the new word “SQUARE” and then draw a Square.

Procedures (“TO” Command) To Draw a Square

PROGRAM EXAMPLE: USE”TO” COMMAND TO DRAW A SQUARE
TO SQUARE; Add 'SQUARE' to the turtle's vocabulary. (Define 'SQUARE' procedure).
REPEAT 4 [ FORWARD 300 RIGHT 90 ]  ;1)
END

SQUARE ; Call 'SQUARE' procedure ;2)

Note: In LOGO, the comments begin with the semi-colon (;) character. All text following the semi-colon is treated as comments and is ignored by the LOGO interpreter. Adding comments helps to explain how the program works. The above program has two comments 1) and 2).

“TO” means that the lines of code between the “TO” statement and the “END” statement define the word “SQUARE”. The word “Square” is a PROCEDURE and it gets added to the turtle’s vocabulary. The turtle now will understand that if you type “SQUARE” in any future program, the turtle has to draw a square. The “TO” command defines the PROCEDURE what instructions to do.

Detailed Explanation of the Program to Draw a Square

Let us now look at the lines of code in the program above.

Between the start of the “TO” statement and the “END” statement is the main body of the PROCEDURE code (comment 1). The statement

REPEAT 4 [FORWARD 300 RIGHT 90]

defines how a square shape is drawn. Notice that the “FORWARD 300 RIGHT 90” statement is the same as we used to draw the square in an earlier chapter, except in this program, we enclose “FORWARD 300 RIGHT 90” in square brackets and prefix it with the words “REPEAT 4“.


The last statement “SQUARE” (comment 2) in the above program is what is known as calling the procedure or calling the subroutine. When you type “SQUARE” in your program, the code between the “TO” and “END” is called and executed.

Note: In the above program we use the statement “REPEAT“. The REPEAT statement has not been explained yet. This statement is discussed in more detail on the “Loops in LOGO” page.

Procedure to Draw an Octagon

Similar to the above example to define the procedure “SQUARE”, you can make the turtle understand other words. The program below defines a procedure “OCTAGON”.

PROGRAM EXAMPLE: “TO” COMMAND TO DRAW AN OCTAGON
   TO OCTAGON; Define ‘OCTAGON’ procedure.
   REPEAT 8 [ FORWARD 100 RIGHT 45 ] ;1)
   END

   OCTAGON; Call ‘OCTAGON’ procedure ;2)

The above program defines the word “OCTAGON” in statement numbered 1). The last statement numbered 2) calls the procedure “OCTAGON” defined between the words ‘TO’ and ‘END’ and executes it, which will draw an octagon.

Procedure to Draw a Flower (“TO” command)

The following program shows the use of the “TO” command to draw a flower. First, we define a procedure to draw a SQUARE as in the program above. This procedure adds the new word “SQUARE” to the turtle’s vocabulary (statement 1)

Next, we define a new procedure and call it “FLOWER”. The “FLOWER” procedure uses the “TO” command to define a new vocabulary called ‘FLOWER” (statement 2).

PROGRAM EXAMPLE: USE THE”TO” COMMAND TO DRAW A FLOWER
TO SQUARE
  REPEAT 4 [ FORWARD 150 RIGHT 90 ] ; 1) Add ‘SQUARE’ to turtle’s vocabulary
END

TO FLOWER ; Add ‘FLOWER’ to turtle’s vocabulary
  REPEAT 18 [ SQUARE RIGHT 20 ]; 2) Call ‘SQUARE’ procedure.
END

FLOWER ; 3) Call ‘FLOWER’ procedure.

Detailed Explanation of the “DRAW A FLOWER” Program:

The program to draw a flower works as follows.

Statement 1) defines a PROCEDURE to draw a square.

Statement 2) defines a PROCEDURE to draw a flower. The “FLOWER” procedure calls the “SQUARE” procedure.

Flower Procedure

Notice in statement 2), the command “SQUARE RIGHT 20” within the brackets calls the SQUARE PROCEDURE, which draws the SQUARE. Then, the “RIGHT 20” part of the command turns the SQUARE to the RIGHT by 20 degrees.

Also, notice that the [SQUARE RIGHT 20] command has REPEAT 18 in front of it. The REPEAT 18 command repeats the code inside the brackets 18 times.

After the FLOWER PROCEDURE’s first repeat, statement 2) turns the SQUARE right by 20 degrees. On the second iteration of the REPEAT command, this process is repeated. In each iteration of the “SQUARE” call, the FLOWER PROCEDURE turns the SQUARE 20 degrees to the right. This process keeps on going until the SQUARE has been turned RIGHT 18 times.

Statement 3) is our main program. It consists of only one statement “FLOWER”. This statement calls the “FLOWER” procedure defined earlier in the program, which, in turn, calls the “SQUARE” procedure 18 times, each time turning the SQUARE 20 degrees to the right. 

This program is an example of “CALLING THE PROCEDURE”.  If you save the “SQUARE” procedure as discussed on the LOGO Basics page, the  “SQUARE” becomes part of LOGO’s vocabulary. This new word may be used as part of another program. The above program draws a flower as in the figure below.

LOGO Procedures Use to Draw a Flower.
Illustrate the Use of “TO” LOGO Procedures Command to Draw Flower

In summary, programming in LOGO is done by adding new words to the existing vocabulary.  New words are defined using the “TO” command.

Enrichment

Run the following program on your computer to see what the turtle draws,

To NEWFLOWER
REPEAT 36 [ RIGHT 10 SQUARE ]
End

Then write a program to draw a flower using ‘OCTAGON’ as follows:

  1. First, add ‘OCTAGON’ to the turtle’s vocabulary.
  2. Then, write a Procedure to draw a FLOWER by repeating ‘OCTAGON’ 18 times. Name this procedure ‘FLOWEROCTAGON’.
  3. Then call this Procedure ‘FLOWEROCTAGON’.

For further reading about LOGO Procedures (TO command), look at the MIT Media Labs link below:

<a href=”https://el.media.mit.edu/logo-foundation/what_is_logo/logo_primer.html“>

Copyright © 2019 – 2021 softwareprogramming4kids.com

All Rights Reserved

Verified by MonsterInsights