Back to DFS's Pascal Page


Procedures II
Passing Values & Addresses

As the problems you work on get larger, it becomes ever more important to break them down into smaller parts. The smaller parts should each deal with a small task. While there are important advantages, there is also a great danger that appears as programs get larger and larger -- it becomes more and more likely that variables will be inadvertently altered. This can have dire effects on the program and can be extremely difficult to debug.

Pascal and many other high-level programming languages provide a means to avoid accidentally changing the values of variables.

If the value of a variable will not be changed inside a procedure, then a value parameter is used to pass only the value to the procedure called. If the procedure gets only the value and not the location where it is stored, the procedure is unable to change it. This is demonstrated in the procedure PrintingResults. It receives the values for the area, base, and height of the triangle. It does not change them - it only prints them.

On the other hand, if the intent of a procedure is to get values from the user, variable parameters are used. In this case, the addresses of the variables to be filled are sent to the procedure. In this way, Pascal knows where to store the values obtained. This type of parameter is indicated by using the var reserved word inside the parens of the procedure heading to introduce the variable parameter.

In still other cases, values are needed to calculate a new value which is needed in the main line or calling procedure. This is shown with the Calculation procedure, where the base and height are used to calculate the area. Here value parameters are used for the base and height, because they will not be changed, while a variable parameter is used for the area, because the value for the area is needed back in the main line.

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.

© 1998-99 DFStermole
HTMLified: 28 Dec 99