## Pascal

Moderators: None (Apply to moderate this forum)
Number of posts: 14016

This Forum Only

Array as return value... Posted by BIBSoft on 31 Mar 2003 at 12:40 PM
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.

Re: Array as return value... Posted by Phat Nat on 31 Mar 2003 at 4:20 PM
This message was edited by Phat Nat at 2003-3-31 16:21:12

: 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:

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

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

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

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

Phat Nat

Re: Array as return value... Posted by roland48 on 3 May 2003 at 12:12 PM
: 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;

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic