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!
  1. Use the Logo commands forward, left and right to draw on the screen. Use clearscreen to erase your drawing.

  2. Draw an equilateral triangle on your screen. How far does the turtle have to turn at each vertex of the triangle in order to draw the next edge?

  3. Use repeat to draw a small equilateral triangle on your screen.

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

  5. 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.

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

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 tell it -- 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.

To do this we can use a variable named size. When you say triangle 50 the number 50 will be stored in the variable named size. We'll refer to the number stored in size in the Logo procedure 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]
end
Try using this new triangle procedure to draw triangles with different length sides. Be sure to put dots (a colon) in front of the variable name; we'll learn why you have to do this later.  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)
end
  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
end