•
•
•
•
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
![]() |
•
•
Join Date: Nov 2007
Posts: 19
Reputation:
Rep Power: 2
Solved Threads: 0
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.
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
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
You most certainly can return arrays from functions. Here are two ways to solve your problem.
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.
Delphi Syntax (Toggle Plain Text)
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.
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Pascal and Delphi Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- returning arrays from functions (C++)
- Class Passing and Returning Arrays (Java)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: help with random numbers in pascal
- Next Thread: a question for pascal



Linear Mode