Inside Delphi 2006 (Wordware Delphi Developers Library)
You can pass arrays as parameters in functions and procedures, but you cannot specify the range of an array parameter. The following example illustrates a procedure declaration that causes a compilation error.
procedure WriteStrings(Str: array[1..20] of string); begin end;
If you need to pass an array of 20 strings to a procedure, you have to create a new data type and then create a procedure that accepts the new data type as the parameter. To create a new data type, you have to use the reserved word type. The syntax for creating a new data type is:
type TNewDataType = ExistingType;
To create a new data type that represents an array of 20 strings, you have to write something like this:
program Project1; {$APPTYPE CONSOLE} uses SysUtils; type TSmallArray = array[1..20] of string; begin end.
Now that you have a new data type that represents the array, you can successfully pass it as a parameter to a procedure or a function.
type TSmallArray = array[1..20] of string; procedure WriteStrings(StrArray: TSmallArray); var item: string; begin for item in StrArray do WriteLn(item); end;
There is a drawback to this solution: The WriteStrings procedure can only accept a TSmallArray variable and thus we're limited to an array of 20 strings. If you want to work with arrays of different sizes, you have to define an open array parameter.
The syntax of open array parameters is the same as the syntax for declaring a dynamic array:
procedure ProcedureName(ArrayName: array of DataType);
Open array parameters enable you to pass arrays of various sizes to the procedure or function. You can easily pass static and dynamic arrays and custom array types.
Listing 6-9: Open array parameters
program Project1; {$APPTYPE CONSOLE} uses SysUtils; type TSmallArray = array[1..20] of string; var SmallArray: TSmallArray; DynamicArray: array of string; StaticArray: array[1..100] of string; procedure WriteStrings(StrArray: array of string); var item: string; begin for item in StrArray do WriteLn(item); end; begin WriteStrings(SmallArray); WriteStrings(DynamicArray); WriteStrings(StaticArray); end.
Open array parameters allow you to create an array directly in the procedure or function call. This is also known as passing an open array constructor. An open array constructor is a sequence of values or expressions enclosed in brackets and separated by commas. The syntax of the open array constructor in a procedure call is:
procedure ProcedureName([expr_1, expr_2, expr_n]);
To create an array of strings inside the WriteStrings procedure call, you can write this:
WriteStrings(['Open', 'Array', 'Constructor']);
Open array parameters can be very useful when we have to create a utility function or a procedure that can work with a range of values. For instance, if we have to create a function that calculates the average of parameters, the version that can accept an array of values is much more useful than other versions that cannot.
Listing 6-10: Function with an open array parameter
program Project1; {$APPTYPE CONSOLE} uses SysUtils, Math; function Average(num1, num2: Integer): Integer; overload; inline; begin Result := (num1 + num2) div 2; end; { open array functions can't be marked with inline } function Average(nums: array of Integer): Integer; overload; var n: Integer; begin Result := 0; for n in nums do Inc(Result, n); Result := Result div Length(nums); end; begin WriteLn(Average(2, 4)); WriteLn(Average([56, 21, 33, 44, 59])); ReadLn; end.