Jack
Jack

Reputation: 53

Function to return an array of extended in VCL Form Delphi

Returning array using a function in Console type can be done but I am trying to make a function which takes an integer n as input and returns an array of extended in VCL form of Delphi. How can we do that?

Upvotes: 0

Views: 772

Answers (3)

David Heffernan
David Heffernan

Reputation: 613332

In modern versions of Delphi you should use the generic array, TArray<T>. Like this:

function Foo(N: Integer): TArray<Extended>;
var
  i: Integer;
begin
  SetLength(Result, N);
  for i := 0 to N-1 do begin
    Result[i] := i;
  end;
end;

I would stress that it is likely a mistake to be using Extended. This is a rather unusual and badly performing 10 byte floating point type. The type is only available on a limited number of processors. Almost all real world floating point calculations are performed using Single or Double, the 4 byte and 8 byte IEEE-754 floating point data types.

Upvotes: 3

BT64
BT64

Reputation: 1

This is not a VCL/Console issue as function declarations are not dependent on the type of application you are writing.

Delphi (or any Pascal) functions can return any named data type. As long as the array type is declared in the type section, a function can handle it.

eg.

type
  ExtArray : array of extended;

function IntToArray(i:integer):ExtArray;
begin
  ...
end;

Upvotes: 0

Slade
Slade

Reputation: 2453

If my memory serves me correct, this has to be done by re-typing it ie:

type
  TResultType: array of extended;

function DoSomthing(): TResultType
begin
  SetLength(Result, 2);
  Result[0] := 1.2;
  Result[1] := 3.4;
end;

Upvotes: 2

Related Questions