Logo

Introduction

Logo is a programming language that was developed in the 1980s to help people learn basic programming concepts. Logo is a graphics-oriented programming language but was developed at a time when computers were unable to display graphics on their screens so Logo was used to control a robotic device called a ‘turtle’ - the turtle moved over a surface according to the instructions in the Logo programme. When the turtle’s pen was lowered onto the paper the turtle's movements left traces on the paper and when the pen was in the ‘up’ position no trace was left. Nowadays computers are well able to display graphics on their screen so the robotic turtle is no longer necessary and has been replaced by a triangle which moves about the screen but which is still sometimes called a turtle.

Challenge 1: a square of variable size

In the EE3, start logo by going to the Classes folder then Ward then double-click on logo.lgo. When the programme has started notice that there are two windows:

bulletthe top window is the graphics window when the turtle moves around. The dimensions of the graphics window are ???? pixels x ???? pixels and if the turtle goes outside this space it will simply reappear on the opposite side.
bulletthe commander window displays the commands that you have given most recently. At the bottom of the instruction window is the input box where you can type instructions for the turtle – these instructions will be carried out instantly. The problem with giving instructions in the input box is that you cannot save the instructions that you type there or the drawings that you make - to be able to save your work you must use the Editor window (see below).

You can do quite a lot with just three commands: FD to go forward, RT to turn right and LT to turn left. Already, you have probably understood how to make a simple shape like a square. Try typing these lines one by one into the input box at the bottom of the commander window (don' type what is in the brackets):

FD 200   (go forward 200 pixels)
RT 90     (right turn 90°)
FD 200
RT 90
FD 200
RT 90
FD 200
RT 90

It is also possible to have multiple commands in a single line so you could have typed:

FD 200 RT 90 FD 200 RT 90 FD 200 RT 90 FD 200 RT 90

However this is not recommended.

Here are some common logo commands (note the presence of spaces in many commands):

FD 100 go forward 100 pixels
BK 100 go backwards 100 pixels
RT 90 right turn 90° (a right angle)
LT 90 left turn 90°
PU  pen up (no line is left as the turtle moves)
PD pen down (a line is left as the turtle moves)
HOME Go to the home (starting) position (don't forget to precede this with PU if you don't want a line to be drawn back to the home position).
CS Clear screen (erase everything in the drawing window)
PE PENERASE. Once you have issued this command you can erase lines by moving over them in erase mode. To return to normal drawing mode, issue the command PENPAINT or PPT.
HT Hide turtle
ST Show turtle
SETPC [000 255 000] Set the pen color to green (the three whole numbers, each between 0 and 255, give the amount of red, green and blue, respectively)

Challenge 2: a triangle

Try to make the turtle draw an equilateral triangle i.e. a triangle whose three sides are all the same length. Hint: to draw the square you turned the turtle through a full revolution making four turns each equal to 90° (360/4)… If you are unable to complete this exercise ask your teacher for help.

Correcting mistakes

As you did the above exercises, perhaps you made a mistake and are wondering how to correct it? Unfortunately Logo does not have an ‘Undo’ command so the only way to fix a mistake is to draw over the bad line with white ink, as if you were using whiteout. To switch to ‘whiteout mode’ (PENERASE mode) issue the following command: PENERASE (or just PE). After erasing the bad line by drawing over it on white leave PENERASE mode and return to the normal (black ink) mode by typing PENPAINT (or just PPT).

Changing the pen colour

While in PENPAINT mode you are not obliged to draw in black ink. You can choose between millions of colors by issuing a command like SETPENCOLOR [000 255 000] or, in its short form, SETPC [000 255 000]. Note that the number part of this command consists of three numbers which represent red, green and blue respectively. Each number must be a whole number between 0 and 255, with 255 being the brightest. So can you figure out what color the above command would give? It would give bright green since the level  of red is zero, the level of green is maximum and the level of blue is zero. And this one: SETPENCOLOR [255 000 255] ? This would be a combination of bright red and bright blue i.e. bright magenta (a shade of purple). And this one: SETPC [255 255 000] ? In color addition, red + green = yellow. If this surprises you it is because you are thinking of what happens when you mix paint, but that's very different because paint works by subtracting (absorbing) certain colors while your computer screen works by adding colors, so you have to learn new rules for mixing colors. Click here to learn more about adding colors.

Loops

When you typed in the above commands to draw a square you must have found it a bit tedious to type the same commands four times. Aren’t computers supposed to be good at automating repetitive tasks? Yes they are, and you can shorten the above set of commands for drawing a square into a single line like this (before you enter the new command, clear the screen by typing CS):

    REPEAT 4 [FD 150 LT 90]

This is pretty easy to understand, isn’t it – the turtle simply repeats the commands inside the square brackets four times. Computer programmers call such a repetition a loop – it’s a fundamental concept in all programming languages. Notice that you need square brackets, not the normal ones, and pay attention too to putting spaces in the right places. The computer is very fussy about the ‘grammar’ of the instructions, which in computer programs is also called the ‘syntax’. In the above line you have also noticed that we drew a smaller square than before, only 150 pixels along each side, and that we turned left instead of turning right.

Procedures

So now we know how to tell Logo to draw a square for us any time we want with just a single line of instructions but the line is rather long and hard to remember – wouldn’t it be nice if we could make Logo draw a square simply by issuing the command SQUARE? Try typing that now into the input box and Logo will give you a message back that it does not understand that word. So we will try to explain to Logo what that word means by making a procedure called SQUARE. Here’s how to do it: In the input box type EDIT "SQUARE            (notice the double quote)

This will open the Editor window where we will build our procedure – you can see that the window already contains two commands:

TO SQUARE
END

Insert a blank line between the two lines given and then copy the long REPEAT command above into the blank line. Now save your procedure and exit the editor by choosing Save and Exit in the editor’s File menu. Note that you have only ‘saved’ the procedure into the Logo program – you have not saved it to disk. However procedures can be saved to disk by choosing Save from the File menu of the main Logo window. Logo saves all the procedures into a single file with the extension .LGO . Only procedures can be saved by Logo, never the instructions that you type into the input box.

WARNING: If you load a Logo file which contains a procedure with the same name as a procedure that is already open then the open procedure will be lost and replaced with the one from the disk – you must name your procedures carefully to make sure this does not happen.

Now that you have created your SQUARE procedure you can run it at any time by typing SQUARE into the input box and pressing ENTER.

Variables

Of course our SQUARE procedure is rather limited because it always produces squares with the same size (150 pixels on each side). Wouldn’t it be nice if we could choose how big the square is to be each time we ask for a square? We will make this possible by inventing a variable called SIZE. A variable is like a container or a box – the contents of the box can change. Variables are a very important part of all programming languages.

Type EDIT "SQUARE into the input box and press ENTER to open the procedure in the Editor.

Now modify the procedure until it looks like this:

TO SQUARE :SIZE
REPEAT 4 [FD :SIZE LT 90]
END

Notice that in the first line we have included the name of our variable – this tells the procedure that when we run the procedure later the command SQUARE will be followed by a number which is to be placed into the variable called SIZE. This number which accompanies the command is called its argument. In the second line we see :SIZE. The colon in front of the variable name tells the computer to retrieve the number that is stored inside the variable – this number replaces the 150 that we used to have as the size of the square so it becomes the new size. If the colon were not present then Logo would not recognize SIZE as a variable and would treat it as ordinary text, causing an error here.

When you have finished, save the procedure, exit the editor and try running your new procedure by typing SQUARE 200. This should cause a square to be drawn with sides of 200 pixels.

Challenge 3: a pentagon

Try to make a procedure called PENTAGON which draws a regular pentagon (5 sides) with a variable size that can be included as an argument of the PENTAGON command as for the square.

Challenge 4: a polygon

Try to make procedure called POLYGON to draw a regular polygon. Use a variable called :sides so that the user can request a polygon with any desired number of sides, for example by typing POLYGON 7. Try to ensure that polygons with many sides can be drawn without going out of the available space (difficult!!)

Challenge 5: a flower

Make a procedure called FLOWER to generate the pretty shape below. At first glance the pattern may seem very complex, with many different shapes, but look again and you will see that this pattern actually consists simply of 10 square 'petals' so all your procedure needs to do is draw a square, then rotate a bit, then draw another square etc. The neat thing about this procedure is that it will contain the word 'square' - it will be the first time that you make a procedure which uses or 'calls' another procedure. That's right - you can have one procedure inside another one.

Challenge 6: a house

Make a procedure called HOUSE which makes a simple house by drawing a triangle on top of a square. Your procedure should call the SQUARE procedure and the TRIANGLE procedure that you have already made. You'll need to think about what you have to do to make sure the triangle is drawn in the right place!

Challenge 7: a village

Make a procedure called VILLAGE which draws three evenly-spaced houses as shown below. Your VILLAGE procedure should call the HOUSE procedure three times. At some point you'll need to use the pen up (PU) and pen down (PD) instructions so that you don't leave lines between the houses.

Challenge 8: a calculator (extra credit)

To get more practice with variables, let’s try to use Logo as a calculator. Make the following procedure called MULTIPLY:

to multiply
   ; A sample routine to demonstrate "questionbox"
   ; and "first" to extract a number from user input.
   make  "Number1  first  questionbox [User Input] [Enter the first number]
   make  "Number2  first  questionbox [User Input] [Enter the second number]
   make  "Answer  :Number1 * :Number2
   home
   cs
   ht
   label  :Answer
end

Produces …..

 

NOTES:

bullet Lines beginning with a semicolon are comments which are ignored by Logo but which make the program easier to understand for you, the programmer.
bullet Double quotes (speech marks) must be placed before the variable name if the variable is going to be written to (modified) rather than read.
bullet The variable names can be just about anything.
bullet The word "first" before "questionbox" is a MSW Logo function that extracts the first value entered by the user as a number.
bullet The star is the symbol programmers use to represent multiplication.
bullet The command 'label' causes text to be displayed in Logo's graphics area.
bullet If you have used Visual Basic you will realize that the Logo questionbox is like the Visual Basic inputbox.

More Information

For more information on Logo please click one of the links below to open the corresponding document. The documents should open in your browser - you do not need to print either of them (if you absolutely want to print one of them please use your own printer not a school printer).

Welcome to the Turtle World of Logo This is a 15 page PDF document (150KB) that is probably too young for secondary school students.

MSW Logo, A Simplified Reference. This is a 22 page Word document (150KB) that is appropriate for secondary school students.

Try also:

bullet

The Logo Foundation - http://el.media.mit.edu/Logo-foundation/

bullet

Download MSW Logo from http://www.softronix.com

bullet

Jim Fuller’s MSW Logo Resource Page http://www.southwest.com.au/~jfuller/mswlogo/mswlogo.html