Logo Lab #7b: Using Logo Procedures and Variables
Introduction to Procedures
- Procedures are separate "mini" programs that perform a specific task.
For example a procedure TO CIRCLE will draw a circle on the screen starting at the turtle's current position.
It can be used any more than once to draw the circle.
- Procedures are written in the procedure screen that is accessed from the Pages menu or the shortcut <Ctrl>F
- Procedures have the following form
TO <name> <variables>
--instructions
.
.
END
An Example:
TO TRIANGLE
REPEAT 3 [FD 100 RT 120]
END
- The procedure is used by typing the name at the command screen. The results of the procedure are displayed on the graphics screen.
Example #1: Drawing Regular Figures
TO PENTAGON
REPEAT 5 [FD 30 RT 72]
HT
END
Write a procedure to draw a square and show that it works by drawing it on the graphics screen.
Write another program to draw a hexagon and show that it works by drawing it on the graphics screen.
Write another procedure that approximates a circle and show that it works by drawing it on the graphics screen.
Example #2: Draw a square with lines that divide its area into four equal squares
- On the procedure page type in the TO Square procedure:
TO SQUARE
REPEAT 4 [FD 50 RT 90]
END
- Copy and Draw with the following procedure SQUARE4:
TO SQUARE4
SQUARE
BK 50
RT 90
FD 50
LT 90
FD 50
LT 90
FD 100
RT 90
FD 50
RT 90
FD 50
RT 90
FD 100
RT 90
FD 50
RT 90
FD 90
END
- Copy and Draw the squares with the following procedure:
TO SQUARE42
SQUARE
RT90
SQUARE
RT 90
SQUARE
RT 90
SQUARE
END
- Copy and Draw the squares with the following procedure:
TO SQUARE43
REPEAT 4 [SQUARE RT 90] Loop to Repeat the Square 4 times followed by RT 90 Deg
END
- Notice that you have produced the same drawing with three different procedures. What procedure do you prefer? Why?
Example #3: Triangle --> Hexagons --> SnowFlakes
- First write a procedure to draw a triangle.
TO TRIANGLE
ST
REPEAT 3 [FD 20 RT 120]
HT
END
- Next use that Triangle procedure in another procedure to draw a hexagon.
TO HEX
REPEAT 6 [TRIANGLE RT 60]
END
- Use the HEX procedure to in the snowflake procedure.
TO SNOWFLAKE
REPEAT 6 [HEX FD 40 HEX BK 40 RT 60 HEX]
END
- Write a similar example Curve --> Trillium --> More Trilliums
- Write a procedure to draw the Curve.
- Write another procedure to draw the trillium that uses the CURVE procedure.
- Write another procedure to draw the pattern that uses the TRILLIUM procedure.
- Experiment with different patterns and hand in a listing of your procedures and copies of your drawings.
Introduction to Variables
- Variables are a method for making a procedure more flexible.
Rather than having a procedure for an equilateral triangle of a single side length,
a variable can make a procedure that draws a triangle on any side length.
- Variables are symbols for numeric values that can be changed
:L might be defined as to have value of 5 but it can be changed to 7 and later to 8
- Defining variables
Variable defined with the MAKE statement
MAKE "L 25 The variable L is assigned a value of 25
MAKE "K :K + 1 The variable K is incrementd by 1
- Variables can be used in statements
MAKE "NUM 5 The variable NUM is assigned a value of 5
REPEAT :NUM [FD 100 RT 72] Draws a pentagon
- Variables can be used in procedures
The procedure TO SQUARE has a variable :LEN added
TO SQUARE :LEN
REPEAT 4 [FD :LEN RT 90]
END
To call the procedure with a side of length 300
SQUARE 300
Exercise #1: Making the Square procedure more general
- Defining and using a variable
- At the command screen define the variable LENGTH and assign it the value of 150
MAKE "LENGTH 150
- Use this variable to draw a square with side length 150
REPEAT 4 [FD :LENGTH RT 90]
- Draw the square
- Change the length to 200 and redraw the square
- Defining a procedure with variables
- Type the following procedures on a new page
TO SQUARE
REPEAT 4 [FD 100 RT 90]
HT
END
Try SQUARE This draws a square of side length 100
- Revise the procedure to let the user enter a length for the side of the square.
TO SQUARE2 :L L is the length of the side
REPEAT 4 [FD :L RT 90]
HT
END
Try: SQUARE2 150
SQUARE2 200 What does the L value produce?
- Make the procedure more general by adding another variable so that the user can enter the Length of the side and the angle of the turn.
TO SQUARE3 :L :A L is the side length, A is the Angle turned
ST
REPEAT 4 [FD :L RT :A]
HT
END
Try: SQUARE3 40 90
SQUARE3 30 45 Why doesn't this polygon close?
- Again modify the procedure to make it more general with a third variable that indicates the number of sides of the polygon.
TO SQUARE4 :N :L :A N is the number of sides, A is the Angle, L is the side length
ST
REPEAT :N [FD :L RT :A]
HT
END
Try: SQUARE4 4 40 90
SQUARE4 5 50 70
SQUARE4 6 30 60
- Save this project as SQUARES
Exercise #2: Creating a General Regular Polygon procedure
- Create a procedure for drawing regular polygons
Remember the turtle needs to:
turn 120 deg (or 360/3) for a triangle
turn 90 deg (or 360/4) for a square
turn 72 deg (or 360/5) for a pentagon
turn 60 deg (or 360/6) for a hexagon
for an N sided polygon the degrees of turn will be 360/N
The procedure should have the following parameters:
TO POLY :N :L N is the number of polygon sides L length of the sides
.
.
END
Try the POLY procedure for a square and a decahedron
- Use the procedure POLY to draw combinations of polygons
Write a procedure TWIST that uses the POLY procedure that will rotate polygons about a vortex (a corner).
Some examples of rotating a rectangle are shown below.
Hand in a listing of the procedure TWIST and several examples of its drawing.
Exercise #3: Redo the program to produce a growing Triangle
- In last week exercises you wrote instructions to draw inscribed triangles of increasing side length. Shown in the figure below:
- Write a procedure that will draw the design above and let the user enter the increase in length of the side,
the number of sides of the figure, and the number of sides to be drawn.
- Hand in a listing of the procedure and several examples of a triangle, rectangle, and a hexagon.
Return to Projects Index