Returning arrays

Reply

Join Date: Nov 2007
Posts: 19
Reputation: Artmann is an unknown quantity at this point 
Solved Threads: 0
Artmann Artmann is offline Offline
Newbie Poster

Returning arrays

 
0
  #1
Nov 12th, 2007
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.
-Signed Artmann
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Returning arrays

 
0
  #2
Nov 12th, 2007
You most certainly can return arrays from functions. Here are two ways to solve your problem.
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 19
Reputation: Artmann is an unknown quantity at this point 
Solved Threads: 0
Artmann Artmann is offline Offline
Newbie Poster

Re: Returning arrays

 
0
  #3
Nov 12th, 2007
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.
-Signed Artmann
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC