Back to DFS's Pascal Page


Program Development: Bottom-Up

Developing a non-trivial program can be a major undertaking. To make the work as painless as possible, it is best to have a method of attack. There are two techniques which are commonly used: bottom-up and top-down.

Both require pre-planning. This is best done by creating pseudocode.

Typically, when a problem is posed, you will have an idea of what the output should be like. From there you determine what sort of information is required to develop the output. Then you can fill in the middle of your algorithm -- how to process the input so you can produce the output.

Alternatively, you can brainstorm the kinds of tasks that will be required, making a list as they come to mind. These can then be organized into a coherent algorithm, thus producing the requisite pseudocode.

Once the pseudocode has been written, it will be much easier to write the individual procedures/functions to perform the required tasks.

For the purposes of this discussion, let us consider the overworked area-of-a-triangle program. Here is simple pseudocode:

  1. Introduction
  2. Get values for the base and height
  3. Calculate the area
  4. Print results

We will be developing the program discussed in Procedures II. There you will find a complete presentation of the parameters used.

Bottom-Up

Starting at the End

To begin we will write a simple program which calls a procedure to print out the results. (The main line of this simple program is know as the driver.) The procedure needs the values for the base, height and area. The values do not need to be altered inside the procedure, so value parameters will be used. To test the procedure, we will hard-set the three variables in the main line.

program Test_PrintingResults(output);

Procedure PrintingResults(b, h: integer; A: real);
 begin
  writeln;
  writeln('The area of a triangle with a base of ', b : 0, ' units and');
  writeln('a height of ', h : 0, ' units is ', A : 0 : 1, ' square units.')
 end; {procedure PrintingResults}

var
  base: integer;
  height: integer;
  Area: real;

begin {main line}
 base := 7;
 height := 5;
 Area := 17.5;
 PrintingResults(base, height, Area)
end.

Working Backwards I: Doing the Calculation

Next, we will write the procedure to calculate the area. This procedure will not alter the variables for base and height, but the area will be changed, since that is the purpose of the procedure. Thus b and h are value parameters and A is a variable parameter.

Since we are calculating a value for the area, only base and height will be hard-set in the main line. To ensure that the values for the base and height have not been altered and that the correct value has been calculated for the area, their values are printed in the main line after the procedure call.

program Test_Calculation(output);

procedure Calculation(b, h: integer; var A: real);
 begin
  A := b * h / 2
 end; {procedure Calculation}

var
  base: integer;
  height: integer;
  Area: real;
 
begin {main line}
 base := 7;
 height := 5;
 Calculation(base, height, Area);
 writeln('base = ', base);
 writeln('height = ', height);
 writeln('Area = ', Area:0:1)
end.

Alternatively, we could add the definition of Calculation to the previous program (thus building the final product step by step) and use the following main line:

begin {main line}
 base := 7;
 height := 5;
 Calculation(base, height, Area);
 PrintingResults(base, height, Area)
end.

Working Backwards II: Inputting the Data

It is now time to create the procedure for inputting data. Both the base and height of the triangle have heretofore been hard-set in the main line. Since their values will now be obtained from the user of the program, the parameters for the procedure need to be variable ones.

program Test_InputtingData(output);

procedure InputtingData(var b, h: integer);
 begin
  write('What is the length of the triangle''s base? ');
  readln(b);
  write('What is the triangle''s height? ');
  readln(h)
 end; {procedure InputtingData}

var
  base: integer;
  height: integer;
  Area: real;

begin {main line}
 InputtingData(base, height);
 writeln('base = ', base);
 writeln('height = ', height);
end.

Once we are certain that this small test program works, we can add the InputtingData procedure to our growing program and use the following main line to test it.

begin {main line}
 InputtingData(base, height);
 Calculation(base, height, Area);
 PrintingResults(base, height, Area)
end.

Finishing Up: First Things Last

All that remains is for us to write the Introduction procedure and add a call to it at the beginning of the main line. The end result of our programming exercise is the program listing given below.

program TriangleAreaProcedures(input, output);

procedure Introduction;
 begin
  writeln('This program calculates and prints the area of a triangle');
  writeln('after you enter its dimensions.');
  writeln('When asked to, type in a dimension and hit the RETURN key.');
  writeln
 end;  {procedure Introduction}

procedure InputtingData(var b, h: integer);
 begin
  write('What is the length of the triangle''s base? ');
  readln(b);
  write('What is the triangle''s height? ');
  readln(h)
 end; {procedure InputtingData}

procedure Calculation(b, h: integer; var A: real);
 begin
  A := b * h / 2
 end; {procedure Calculation}

procedure PrintingResults(b, h: integer; A: real);
 begin
  writeln;
  writeln('The area of a triangle with a base of ', b : 0, ' units and');
  writeln('a height of ', h : 0, ' units is ', A : 0 : 1, ' square units.')
 end; {procedure PrintingResults}

var
  base: integer;
  height: integer;
  Area: real;

begin {main line}
 Introduction;
 InputtingData(base, height);
 Calculation(base, height, Area);
 PrintingResults(base, height, Area)
end.

Conclusion

The techniques discussed above are not used to the exclusion of others. In fact, the method outlined on the Top-Down page is frequently utilized to create an overview of the whole project, after which individual procedures are developed. This combined approach is especially important when a group of programmers is working on a single project.


© 2001 DFStermole
Created: 13 Nov 01
Last modified: 28 Nov 01