943,929 Members | Top Members by Rank

Ad:
Nov 12th, 2007
0

Returning arrays

Expand Post »
Hello.

I'm studying Pascal in school atm and have come over a bit of a problem with a program I've been asked to write.

The task is too make a calendar. My calendar uses an array with records too store data about the different days. The program uses a procedure to fill the global array with data.
Here comes the problem, my teacher don't like global variables being used inside procedures or functions.

As I come from a Java background I'm really frustrated over the fact that I can't find any simple way to convert my procedure to a function and return an array.

So I wonder if there is some hidden way to do this or some availabe extension to overcome this problem.

I would really appreciate some help.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Artmann is offline Offline
19 posts
since Nov 2007
Nov 12th, 2007
0

Re: Returning arrays

You most certainly can return arrays from functions. Here are two ways to solve your problem.
Delphi Syntax (Toggle Plain Text)
  1. program u;
  2. {$APPTYPE CONSOLE}
  3. uses
  4. SysUtils;
  5.  
  6. type
  7. tPerson = record
  8. name: string;
  9. age: integer;
  10. end;
  11. tPeople = array of tPerson;
  12.  
  13. var people: tPeople;
  14.  
  15. //procedure getPeople( var result: tPeople ); // Method 1
  16. function getPeople: tPeople; // Method 2
  17. var
  18. s: string;
  19. i: integer;
  20. begin
  21. i := 0;
  22. repeat
  23. setLength( result, i+1 );
  24. write( 'Please enter a name> ' );
  25. readln( result[ i ].name );
  26. write( 'Please enter ', result[ i ].name, '''s age> ' );
  27. readln( result[ i ].age );
  28. write( 'Are there more (yes/no)? ' );
  29. readln( s );
  30. inc( i )
  31. until upCase( s[ 1 ] ) = 'N'
  32. end;
  33.  
  34. begin
  35. // Method 1
  36. //getPeople( people );
  37.  
  38. // Method 2
  39. people := getPeople;
  40.  
  41. writeln( 'You entered ', length( people ), ' people.' )
  42. 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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 12th, 2007
0

Re: Returning arrays

Thanks for your help, this will help me out a lot. I think I've read a simular example earlier somewhere but I had totally forgot about that.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Artmann is offline Offline
19 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: help with random numbers in pascal
Next Thread in Pascal and Delphi Forum Timeline: a question for pascal





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC