WHAT IS 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 PENTAGONREPEAT 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 SQUARE 4:
TO SQUARE 4
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 SQUARE 42
SQUARE
RT 90
SQUARE
RT 90
SQUARE
RT 90
SQUARE
END - Copy and Draw the squares with the following procedure:
TO SQUARE 43
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 --> Snow Flakes
- 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
No comments:
Post a Comment