Array as return value... - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Array as return value...

BIBSoftBIBSoft Posts: 3Member
Hi! Sorry for this newbie question but I need help with some code just to show me how to create a function which return an array. I can't find any examples anywhere. Please help me.

Comments

  • Phat NatPhat Nat Posts: 757Member
    [b][red]This message was edited by Phat Nat at 2003-3-31 16:21:12[/red][/b][hr]
    : Hi! Sorry for this newbie question but I need help with some code just to show me how to create a function which return an array. I can't find any examples anywhere. Please help me.

    Don't be sorry. This is a good question. Here is the esiest way to do it:

    [code]
    VAR
    TheArray : Array[0..15] Of String;

    PROCEDURE Whatever(Num : Byte; S : String; [b]VAR[/b] MyArray : Array Of String);
    Begin
    MyArray[Num] := S;
    End;

    Begin
    Whatever(1,'Hello!',TheArray);
    WriteLn(TheArray[0]);
    WriteLn(TheArray[1]);
    WriteLn(TheArray[2]);
    End.
    [/code]

    Make sure that you have the [b]VAR[/b] statement in there or else the data will not be returned to your array.

    Phat Nat



  • roland48roland48 Posts: 23Member
    : Hi! Sorry for this newbie question but I need help with some code just to show me how to create a function which return an array. I can't find any examples anywhere. Please help me.
    :
    :
    If you want a function and not a procedure with Var-Parameters, you
    have to use pointers:

    Type
    TArray=Array[0..10] of string;
    PArray=^TArray;

    Function ReturnArray:PArray;
    Var
    P:PArray;
    loop:word;
    begin
    New(P);
    For loop:= 0 to 10 do
    P^[loop]:='Hallo!';

    ReturnArray:=P;
    end;







Sign In or Register to comment.