Back to DFS's Pascal Page


ONE-DIMENSIONAL ARRAYS II

Using Procedures & Functions

Pascal allows arrays to be passed as parameters to procedures and functions. However, the type specification for the array must be simple, i.e., it must be one defined in the type section. This page will present a reworking of the program in One-Dimensional Arrays I to demonstrate how this is done.

A complete sample program is provided at the end of this file.

Programmer-Defined Type

In Pascal, the programmer is permitted to define data types that better reflect the reality he is trying to simulate in his program. Here we will define a type called int100type which will be used for the array in which we plan on storing up to 100 integer values.

type
  int100type = array [1..100] of integer;

Note the use of type at the end of int100type which serves as a reminder that it is actually a programmer-defined type. This type definition must be done globally so that it is accessible when declaring general variables and corresponding formal parameters and local variables in procedures and functions. int100type is then considered to be a simple type which can be used anywhere in the program.

The local-to-main array called scores can now be declared using this simple, programmer-defined type.

scores : int100type;

This same data type must be used in all headings of procedures and functions which will have access to the scores array. In the program included here, there are three such headings.

Procedure GetArrayValues ( var arr : int100type; var HowMany : integer);
Function GetHighest ( arr : int100type; HowMany : integer) : integer;
Procedure ReportFindings (arr : int100type; HowMany, high : integer);

Please note that the names of the routines serve as a straightforward mirror of pseudocode which could be written for this program.

  1. Get values for the array;
  2. Get the highest value in the array;
  3. Report the highest value to the user.

Just as with other procedure and function parameters, arrays can be received as either value or variable formal parameters. You should take care to ensure that you use variable parameters only when you intend to permit values to be changed inside the routine you are calling.

In this program, the values in the scores array are initialized by hard-setting them in the procedure GetArrayValues. In a more useful program, they would be initialized by asking the user to type them in or by reading them from a file. Consequently, when we are initializing the elements, we must declare the array as a variable parameter.

Procedure GetArrayValues ( var arr : int100type; var HowMany : integer);

On the other hand, when we are just examining the values in the array and not changing them, we should use value parameters, since we don't want to accidentally allow ourselves the opportunity to inadvertently change the values.

Function GetHighest ( arr : int100type; HowMany : integer) : integer;
Procedure ReportFindings ( arr : int100type; HowMany, high : integer);

Remember to use different identifiers for the actual and formal parameters. This practice serves two purposes:

  1. It prevents confusion on your part about the scope of the identifiers, thus reducing the risk of side effects (unwanted, and often dangerous, changes to variables).
  2. It reminds you that the routine can be called using different actual parameters, i.e., the routine is reusable.

Sample Program

This is a program which initializes an array of integer scores with 5 values and then finds and prints the highest score.

Program FindHighest (output);
type
  int100type = array [1..100] of integer;

Function GetHighest ( arr : int100type; HowMany : integer) : integer;
{ Assumes that array has AT LEAST one element }
var
  high : integer;
  i : integer;
begin
  { initialize high value }
  high := arr[1];

  { look for higher score,
    replacing value of High, if higher one found }
  for i := 2 to HowMany do
    if ( arr[i] > high) then
      high := arr[i];
  GetHighest := high
end;

Procedure GetArrayValues ( var arr : int100type; var HowMany : integer);
begin
  { initialize counter }
  HowMany := 0;

  { initialize array & count elements }
  HowMany := HowMany + 1;
  arr[HowMany] := 55;
  HowMany := HowMany + 1;
  arr[HowMany] := 74;
  HowMany := HowMany + 1;
  arr[HowMany] := 45;
  HowMany := HowMany + 1;
  arr[HowMany] := 88;
  HowMany := HowMany + 1;
  arr[HowMany] := 67;
end;

Procedure ReportFindings (arr : int100type; HowMany, high : integer);
var
  i : integer;
begin
  write (high, ' is the highest value out of ');
  for i := 1 to HowMany - 1 do
    begin
      write ( arr[i], ', ')
    end;
  writeln( 'and ', arr[HowMany], '.')
end;

var
  scores : int100type;
  num_scores : integer;
  high_score : integer;
  i : integer;
begin {Main Line}
  GetArrayValues(scores, num_scores);
  high_score := GetHighest(scores, num_scores);
  ReportFindings(scores, num_scores, high_score)
end.

Back to Top


© DFStermole 2000
Created 6 May 2000