Jump to content
A-man

function returning an array

Recommended Posts

Is it possible to have a function return a 1 dimensional array? I have this:

function arr(): array of integer;
begin
 result[0] := 0;
 result[1] := 1;
 result[2] := 2;
 result[3] := 3;
end;

// later i have this

blah := arr();

 

I'm just trying to get the fundamentals down. It doesn't throw any syntax errors, so it seems like it should work. I also noticed that the function doesn't show up in the function list if it is an array of (something).

Link to comment
Share on other sites

Yehp:

 

function Test: array of Integer; // Can be replaced with TIntArray, both ways work.
begin
 Result := [1231, 342, 53463, 2353];
 { Other way:
 SetLength(Result, 4);
 Result[0] := 1231;
 Result[1] := 342;
 Result[2] := 53463;
 Result[3] := 2353;
 }
end;

var
 tmp: TIntArray;

begin
 ClearDebug;
 tmp := Test;
 WriteLn('tmp: ' + TIAToStr(tmp));
 SetLength(tmp, 0);
end.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...