Tutorial - Arrays as Formal Parameters

Passing Arrays to Routines

When passing an array to a routines ( either a Function or a Shape ) it is quite common to use an open array as a formal parameter. In these cases it is important that the routine can acquire information on whether the array has actually be allocated and, if so, how big it is.

Existence of an Array

The existence of the array can be determined by comparing the parameter with Null.

	If array <> Null Then
		[= do something with the array =]
		. . .
	EndIf;

Size of an Array

These is a special pair of functions to return the low value of the index, ArrayLow(), and high value of the index, ArrayHigh(), of a given array. The routines take the array and the number of the index ( counting from 1 ). As it is possible to pass a fixed array as open array parameter you should not assume the the low index is always 1. These routines can only return values for Number type indexes. As these are the only indexes that can be open this is not a problem.

Function Logical TestArray(Const Ref MyOpenArray array)
    Begin
	If array <> Null Then
		Output ArrayLow(array, 1);
		Output ArrayHigh(array, 1);
		Return True;
	Else
		Output "No array allocated";
		Return False;
	EndIf;
    End;

Using Const and Ref

In the above example we specified the formal parameter to be 'Const Ref'. If we are not going to be modifying the data within the array we can pass the array by reference. If we did not have this attribute then the system would make a copy of the array to be used by the routine - this would be inefficient. Adding the constant attribute will result in the compiler generating an error it you do try altering the data ( it is quite possible that you do want to alter the data, in which case just omit the 'Const' keyword ).