How to return array from a Delphi function? How to return array from a Delphi function? arrays arrays

How to return array from a Delphi function?


unit Unit1;interfaceuses SysUtils;type  TStringArray = array of string;function SomeFunction(SomeParam: integer): TStringArray;...implementationfunction SomeFunction(SomeParam: integer): TStringArray;begin  SetLength(result, 3);  result[0] := 'Alpha';  result[1] := 'Beta';  result[2] := 'Gamma';end;...end.

The golden rule is that the interface section of a unit describes the data types used by the unit, and the types, classes and functions (their signatures) that reside in the unit. This is what all other units see. The implementation section contains the implementation of the classes and functions. This is not visible to other units. Other units need to care about the interface of the unit, the 'contract' signed by this unit and the external unit, not the 'implementation details' found in the implementation section.


If you Delphi is fairly recent, you don't need to declare a new type, by declaring it as TArray<String>.

Example copied and pasted from the answer above:

unit Unit1;interfacefunction SomeFunction(SomeParam: integer): TArray<String>;implementationfunction SomeFunction(SomeParam: integer): TArray<String>;begin  SetLength(result, 3);  result[0] := 'Alpha';  result[1] := 'Beta';  result[2] := 'Gamma';end;end.