[comp.lang.pascal] Conformant Arrays

scl@virginia.acc.virginia.edu (Steve Losen) (12/22/87)

Conformant arrays simply allow you to pass arrays of varying
sizes to the same routine.  The arrays that you pass to
the routine must have the same number of dimensions, but the
sizes of each dimension can be different.
In pascal the compiler knows the number of dimensions and the
size of each dimension of an array whenever it is passed as a
parameter to a function.  Thus when you call a routine that takes
a conformant array parameter, you simply pass the array and the
compiler invisibly supplies the declared sizes of the dimensions
to the routine.

Example:

program test (input, output);

var
	x : array [1..10] of integer;
	y : array [-10..20] of integer;

{ This procedure uses a conformant array.  Note that the compiler will }
{ invisibly supply values for "first" and "last" when we call "sum". }
{ The compiler will allow you to pass an array of any length to sum provided: }
{ 	array has one dimension }
{   array is indexed by an integer }
{   array contains integers }

function sum(a : array [first..last: integer] of integer) : integer;
	var i : integer;
begin
    sum := 0;
	for i := first to last do
		sum := sum + a[i];
end;

begin { test }

	{ assume we initialize x and y  here }
	writeln (sum(x));	{ note that compiler supplies size of x, not programmer! }
	writeln (sum(y));
end.

The programmer has no control over the parameters "first" and "last" in
the above example, so pascal has no equivalent to the following
FORTRAN fraud.

subroutine proc (x, n)
integer x(n)
...
end
...
integer a(20)
call proc (a(3), 5)

Conformant arrays are included in the ISO pascal standard at level 2, but
not at level 1.
-- 
Steve Losen
University of Virginia Academic Computing Center