You most certainly
can return arrays from functions. Here are two ways to solve your problem.
program u;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
tPerson = record
name: string;
age: integer;
end;
tPeople = array of tPerson;
var people: tPeople;
//procedure getPeople( var result: tPeople ); // Method 1
function getPeople: tPeople; // Method 2
var
s: string;
i: integer;
begin
i := 0;
repeat
setLength( result, i+1 );
write( 'Please enter a name> ' );
readln( result[ i ].name );
write( 'Please enter ', result[ i ].name, '''s age> ' );
readln( result[ i ].age );
write( 'Are there more (yes/no)? ' );
readln( s );
inc( i )
until upCase( s[ 1 ] ) = 'N'
end;
begin
// Method 1
//getPeople( people );
// Method 2
people := getPeople;
writeln( 'You entered ', length( people ), ' people.' )
end.
Uncomment the method you want (the procedure/function body was written so that it will work with whichever header you uncomment).
The trick is that everything in Pascal must be strongly typed. Notice how the array itself has a specific type. Oh yeah, this is also a
dynamic array...
Hope this helps.
Last edited by Duoas; Nov 12th, 2007 at 2:32 pm.
Reputation Points: 1140
Solved Threads: 229
Postaholic
Offline 2,039 posts
since Oct 2007