User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Pascal and Delphi section within the Software Development category of DaniWeb, a massive community of 456,234 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,817 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Pascal and Delphi advertiser: Programming Forums
Views: 1863 | Replies: 2
Reply
Join Date: Nov 2007
Posts: 19
Reputation: Artmann is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Artmann Artmann is offline Offline
Newbie Poster

Returning arrays

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Returning arrays

  #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  
Join Date: Nov 2007
Posts: 19
Reputation: Artmann is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Artmann Artmann is offline Offline
Newbie Poster

Re: Returning arrays

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Pascal and Delphi Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Pascal and Delphi Forum

All times are GMT -4. The time now is 5:11 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC