LOGO Procedures

The purpose of this worksheet is to familiarize you with procedures, subprocedures, variables and procedures with inputs. Write your own programs but feel free to ask a neighbor for help!
Use the Logo commands forward, left and right to draw on the screen. Use clearscreen to erase your drawing.
  1. Write down the instructions you would give Logo to cause it to draw an equilateral triangle. How far should the turtle rotate before starting the next side of the triangle? Feel free to stand up and act out the turtle's motions.










  2. Check your work; have Logo draw an equilateral triangle on your screen.

  3. You should notice that the symmetry of the equilateral triangle leads to repetition in your instructions. If you haven't already done so, use repeat 3 to write a single instruction which draws a small equilateral triangle on your screen.

  4. The instruction repeat 4 [forward 50 right 120] draws an equilateral triangle on the screen. Why is your instruction better?

  5. Write a procedure triangle that draws a small triangle on your screen. Ask for help if you're stuck!






  6. Use triangle to draw a triangle on your screen. Then use penup to make the turtle stop drawing. Use commands like forward and right to move the turtle to another location on the screen, use pendown to start drawing again, then draw another triangle or a decoration.

  7. Write a procedure that uses the triangle as a helper procedure to draw a picture. A sample picture is shown below.
    to boat                                                                      
     triangle ;sail                                                              
     left 90 forward 50 left 180 forward 100 left 180 forward 50 right 90 ; hull 
     left 90 forward 100                                                         
     left 90                                                                     
     repeat 10 [triangle penup left 90 forward 25 right 90 pendown] ;waves       

This picture would be nicer if the "waves" were made of smaller triangles. We could write a new procedure to draw smaller triangles, or we could change our triangle procedure so that it draws more than one size of triangle.

How will triangle know what size triangle to draw? We'll rewrite it so it requires input -- we'll say triangle 50 to draw a triangle with side length 50 and triangle 29 to draw triangles with side length 29. (Why 29? Because that's almost equal to 25 times 2 divided by the square root of 3, and it makes the "waves" line up nicely.)

The Logo commands forward and left already work this way, but triangle doesn't. Try it:

triangle 50.

Logo doesn't know what to do with 50. Your definition of the triangle procedure has to tell Logo what to do with 50 -- to remember this number and use it as the side length.

We need to edit procedure triangle to require an input variable -- let's call it size. When you say triangle 50 the number 50 will be stored in the variable size. We'll look up the number stored in size when we want to tell the turtle how far to move to draw the edge of a triangle.

to triangle :size
 repeat 3 [forward :size right 120]
Try using this new triangle procedure to draw triangles with different length sides. Be sure to a colon or thing " in front of the variable name.  (If you have trouble changing your triangle procedure, ask for help!)
  1. Write a procedure named square which requires one input -- the side length of the square -- and draws a square with that side length. What did you name the variable input to square? Why?

Not surprisingly, variables are useful for mathematical calculations as well as for drawing pictures. The procedure below accepts two inputs -- the side lengths of a rectangle -- and prints the area of the rectangle.

to area :length1 :length2
  print sentence [The area of the rectangle is:] (product :length1 :length2)
  1. Referring to the example above, write a procedure perimeter that accepts two lengths as input and prints the perimeter of the rectangle whose sides have those lengths.





  2. Write a procedure to compute the volume of a rectangular box given its length, width and height.





Bonus: Use your square procedure, penup and pendown in writing a procedure to draw a picture. An example is shown below.
to house                                                               
 square 100 ; frame
 right 90 forward 50 left 90 ; move to door
 square 30 ; door
 penup forward 50 left 90 forward 20 right 90 pendown ; move to window
 square 15 ; window
 penup back 50 right 90 back 30 left 90 pendown ; move back to start