Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function returning array??? 1

Status
Not open for further replies.

Matilde

Programmer
Nov 30, 2001
18
0
0
DK
how do you return an array from a function? ie.

function something(a:integer;b:integer):??? (array[1..2])
begin
...
..
.
end;


?? := something(15,67);
 
Hi,
pass the array as Var Parameter to a Procedure.
For example:

Procedure TForm1.FillMyArray(Var Arr:Array of Integer);
Var i:Integer;
Begin
For i :=0 To Length(Arr)-1
Do Arr[ i]:=i;
End;

Ciao,
Geppo Darkson.
 
I've never seen arrays being returned from a function - I'm not sure if it's possible. The way I have always seen it done is to pass the array into the procedure/function as a reference parameter (i.e. a parameter that will have its' values modified). A reference parameter is indicated by the var keyword. Here's an example of an open array (an array of any length) parameter:
Code:
procedure InitialiseToZero(var Arr: Array of Double)
var 
  i:integer;
begin
  for i:= Low(Arr) to High(Arr) do 
    Arr[i] := 0; 
end;

Hope this helps!

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
Of course it's possible to return an array as function result. Just declare

type
TmyArray = array[0..bla] of integer;

function myFunc(...): TmyArray;


regards
Roderich
 
Rod is correct.
Also you will need to do this if you want to pass multi dimensional arrays INTO a function.
The previous examples use 'open array parameters' and these will only accept single dimensional arrays
Thanks due to Zathras for that.
Steve.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top