Bar_gold.gif 401 bytes

Logo Lab #7b: Using Logo Procedures and Variables

Bar_gold.gif 401 bytes

Introduction to Procedures

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

  2. Procedures are written in the procedure screen that is accessed from the Pages menu or the shortcut <Ctrl>F

  3. Procedures have the following form
      TO <name> <variables>
         --instructions
           .
           .
      END

    An Example:
       TO TRIANGLE
         REPEAT 3 [FD 100 RT 120]
       END

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

  1. On the procedure page type in the TO Square procedure:
      TO SQUARE
         REPEAT 4 [FD 50 RT 90]
      END

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

  3. Copy and Draw the squares with the following procedure:
      TO SQUARE42
         SQUARE
         RT90
         SQUARE
         RT 90
         SQUARE
         RT 90
         SQUARE
      END

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

  5. Notice that you have produced the same drawing with three different procedures. What procedure do you prefer? Why?



Example #3: Triangle --> Hexagons --> SnowFlakes

  1. First write a procedure to draw a triangle.
      TO TRIANGLE
         ST
         REPEAT 3 [FD 20 RT 120]
         HT
      END

  2. Next use that Triangle procedure in another procedure to draw a hexagon.
      TO HEX
         REPEAT 6 [TRIANGLE RT 60]
      END

  3. Use the HEX procedure to in the snowflake procedure.
      TO SNOWFLAKE
         REPEAT 6 [HEX FD 40 HEX BK 40 RT 60 HEX]
      END

  4. Write a similar example Curve --> Trillium --> More Trilliums

    Curve

    Trillium

    Garland
    1. Write a procedure to draw the Curve.
    2. Write another procedure to draw the trillium that uses the CURVE procedure.
    3. Write another procedure to draw the pattern that uses the TRILLIUM procedure.
    4. Experiment with different patterns and hand in a listing of your procedures and copies of your drawings.




Introduction to Variables

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

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

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

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

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

  1. Defining and using a variable
    1. At the command screen define the variable LENGTH and assign it the value of 150
          MAKE "LENGTH 150

    2. Use this variable to draw a square with side length 150
          REPEAT 4 [FD :LENGTH RT 90]

    3. Draw the square

    4. Change the length to 200 and redraw the square


  2. Defining a procedure with variables
    1. 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

    2. 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?

    3. 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?

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

    5. Save this project as SQUARES



Exercise #2: Creating a General Regular Polygon procedure

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

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

  1. In last week exercises you wrote instructions to draw inscribed triangles of increasing side length. Shown in the figure below:



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

  3. Hand in a listing of the procedure and several examples of a triangle, rectangle, and a hexagon.




Bar_gold.gif 401 bytes

           Return to Projects Index

Bar_gold.gif 401 bytes