6.1 Simple Procedures

As programs get larger two important things start to happen. One is that programs need to start being broken up into smaller pieces so that they are more manageable. Another is that you'll want to start reusing pieces of code. Procedures take care of both of those needs.

Turing has quite a large number of predefined procedures. We've used some already. In chapter one we used the locate and color procedures. In chapter two we used procedures to generate random numbers and for graphics (eg. drawline and drawfilloval). In this chapter we will learn how to make our own procedures that we can then use just like the predefined ones.

In chapter two we saw procedures called drawoval and drawbox. Perhaps it would have been nice if there was one called drawtriangle. Let's make one. Suppose we wanted a red triangle that had its three vertices at (100, 100), (150, 150) and (100, 200). A Turing program that would do that would look like this:

drawline(100, 100, 150, 150, red)
drawline(150, 150, 100, 200, red)
drawline(100, 200, 100, 100, red) 

If we want to draw that triangle again and again in a program we would need to keep typing in those three lines over and over again. By using a procedure we will only need to type the lines once. We then call the procedure over and over again with a simple one line statement (drawtriangle). To turn the previous three lines into a procedure you just need to add a line to the beginning of the code and one to the end. You need to tell Turing the name of the procedure. You do that by putting the word procedure followed by an identifier that will be the name of the procedure. At the end of the procedure you put the word end followed by the identifier you used in the first line. Procedures are sometimes called subprograms. This is because they have most of the same features that a program does. For instance they can declare their own variables and constants for their own use. Here is our drawtriangle as a procedure:

procedure drawtriangle
   drawline(100, 100, 150, 150, red)
   drawline(150, 150, 100, 200, red)
   drawline(100, 200, 100, 100, red)
end drawtriangle 

The above program doesn't actually do anything. All we have done is define the procedure. It is ready to be used but a program must call it before the code is actually executed. To call it you use the name of the procedure. Here is a program that calls the procedure twice. Each line is numbered so we can look at the order that lines are executed in:

% definition of procedure drawtriangle starts here
procedure drawtriangle
   drawline(100, 100, 150, 150, red) %1
   drawline(150, 150, 100, 200, red) %2
   drawline(100, 200, 100, 100, red) %3
end drawtriangle
% end of definition of drawtriangle

% program starts executing here
var s : string
% call procedure drawtriangle
drawtriangle                         %4
put "Clear screen"                   %5
get s:*                              %6
cls                                  %7
put "screen cleared - press enter for another triangle" %8
get s:*                              %9
% call procedure drawtriangle again
drawtriangle                         %10 

The lines of this program are executed in the following order: 4 1 2 3 5 6 7 8 9 10 1 2 3.

This procedure does have some limitations. One major one is that it will always draw a red triangle in the same place. Later in this chapter we'll learn how to modify it so that we can draw triangles in different places in different colours.

Exercise 6.1

  1. Explain the difference between a procedure definition and a procedure call.
  2. How does one call a procedure?
  3. In the program shown below, the executable statements are numbered. Use these numbers to indicate the order in which the statements are executed.
    procedure chorus
       put ""                             %1
       put "Ee-igh, ee-igh, oh!"          %2
       put ""                             %3
    end chorus
    
    put "Old MacDonald had a farm,"       %4
    chorus                                %5
    put "And on that farm he had a pig,"  %6
    chorus                                %7 
  4. The procedure definition that follows lacks proper indentation.
    procedure printTable const LARGEST := 10 for i : 1 .. LARGEST put
    i, " ", i*i end for end printTable 
    1. Rewrite the definition with proper indentation.
    2. What does the procedure do?
    1. Write a procedure printRectangle that prints a rectangular pattern of the character SYMBOL that is WIDTH characters wide and HEIGHT characters high. The values of SYMBOL, HEIGHT, and WIDTH should be defined as constants within the procedure. As an example, if we have SYMBOL = '*', HEIGHT = 3, and WIDTH = 8, the procedure should print the following pattern:
      ********
      ********
      ********
      Use for statements to control the printing of the pattern.

    2. Test your procedure in a small program that prints the rectangle, a one-line message, a second rectangle, and second one-line message, and a final rectangle.
  5. Using the following procedures:

    procedure solid
       for i : 1 .. 20
          put "*" ..
       end for
       put ""
    end solid
    
    procedure hollow
       for i : 1 .. 20
          if i = 1 or i = 20 then
             put "*" ..
          else
             put " " ..
          end if
       end for
       put ""
    end hollow 

    1. Write a program that will print the following:
      1. ********************
        ********************
      2. ********************
        *                  *
        *                  *
        *                  *
        *                  *
        *                  *
        *                  *
        ********************
    2. Ask the user how many rows they want the hollow box to have. Print a hollow box with that many rows.