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
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] endTry 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!
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
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