[next] [prev] [prev-tail] [tail] [up]

#### 14.4.5 Open array parameters

Free Pascal supports the passing of open arrays, i.e. a procedure can be declared with an array of unspecified length as a parameter, as in Delphi. Open array parameters can be accessed in the procedure or function as an array that is declared with starting index 0, and last element index High(parameter). For example, the parameter

Row : Array of Integer;

would be equivalent to

Row : Array[0..N-1] of Integer;

Where N would be the actual size of the array that is passed to the function. N-1 can be calculated as High(Row).

Specifically, if an empty array is passed, then High(Parameter) returns -1, while low(Parameter) returns 0.

Open parameters can be passed by value, by reference or as a constant parameter. In the latter cases the procedure receives a pointer to the actual array. In the former case, it receives a copy of the array. In a function or procedure, open arrays can only be passed to functions which are also declared with open arrays as parameters, not to functions or procedures which accept arrays of fixed length. The following is an example of a function using an open array:

Function Average (Row : Array of integer) : Real;
Var I : longint;
Temp : Real;
begin
Temp := Row[0];
For I := 1 to High(Row) do
Temp := Temp + Row[i];
Average := Temp / (High(Row)+1);
end;

As of FPC 2.2, it is also possible to pass partial arrays to a function that accepts an open array. This can be done by specifying the range of the array which should be passed to the open array.

Given the declaration

Var
A : Array[1..100];

the following call will compute and print the average of the 100 numbers:

Writeln(’Average of 100 numbers: ’,Average(A));

But the following will compute and print the average of the first and second half:

Writeln(’Average of first 50 numbers: ’,Average(A[1..50]));
Writeln(’Average of last 50 numbers: ’,Average(A[51..100]));

[next] [prev] [prev-tail] [front] [up]