Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make a function return an array? 2

Status
Not open for further replies.

wizzor

Programmer
Aug 29, 2006
23
FI
Hi,
I'd like to have my nice little function return an array, I'd expect this to be possible, but when I do this...
Code:
function GeneralCalculatePosition(WidthHeight: Integer; spacing: Integer; padding: Integer; multiplier: Integer):Array of Integer;

begin
if multiplier<>0 then
  if multiplier=1 then
  GeneralCalculatePosition := padding
  else
  GeneralCalculatePosition := padding+multiplier*widthHeight+multiplier*spacing
  else
  showmessage('Application error');

end;

I get this..
[Pascal Error] main.pas(259): E2029 Identifier expected but 'ARRAY' found

So, I tried googling this, and the results were slim, someone at a C forum to return a pointer, but I have no idea as to how I should approach this. Are there any other options? Or will I just have to learn pointers now?
 
To return an array you must first declare it as a type

e.g.
type myarray = array[0..2] of integer;

the function can then return that type
Also you are not indexing the result in your example

should be more like this

function myfunc: myarray;
begin
result[0] := padding
result[1] := 2;
// etc
end;


Steve [The sane]: Delphi a feersum engin indeed.
 
Well what do you know, it worked...
Now, a more of a general sort of question regarding arrays.. I'd like to have one that's (At lest in PHP) referred to as an associative array, IOW something along these lines
adult -> 7.00
child -> 4.50
student-> 4.50
etc.

So I could associate a floating point value, with a name. (Ticket prices), the other option is to return two arrays, one string (names) and one float (prices)...
 
make an array of records.

Code:
type TicketType = record
      Name : string[20];
      Value : Real;
     end; 

type TicketArrayType = Array[1..1000] of TicketType;


var TicketArray : TicketArrayType;
....
TicketArray[1].name := 'adult';
TicketArray[1].value := 7.00;
....

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Thanks!
Do you know, when is the memory allocation made to such an array, is memory allocated to an array of 1000 records when the program is ran, or are memory allocations made as they are required (iow, values and names are allocated)?

Also, is there any way of making the array dynamic, as in, making it possible to allocate any number from 1-infinity?

Like, making the type declaration include a variable like this

Code:
...
[b]arrayLength:integer;
arrayLength := 300 //or whatever[/b]
...
type TicketType = record
      Name : string[20];
      Value : Real;
     end;

type TicketArrayType = [b]Array[1..ArrayLength][/b] of TicketType;


var TicketArray : TicketArrayType;
....
TicketArray[1].name := 'adult';
TicketArray[1].value := 7.00;
....
 
my code sample uses fixed length, so it will consume 1000x SizeOf(TicketType) Bytes.
you can have dynamic arrays like this:

Code:
type TicketType = record
      Name : string[20];
      Value : Real;
     end; 

type TicketArrayType = Array of TicketType;


var TicketArray : TicketArrayType;
....
SetLength(TicketArray, 1000); // reserve 1000 records space
TicketArray[0].name := 'adult'; // be aware that dyn. arrays always have zero based index
TicketArray[0].value := 7.00;
....

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 

Next you will probably want to code some way to search/lookup data in your array.

Instead of using array/records, take a look at using TStringList/objects.

You can define your own object (essentially a record on steroids) to contain any type of data you need for one element, then add an instance of that object to the string list while setting the string value to whatever it is that you need to lookup on.

That way you can use methods of TStringList to perform the lookup/find operations you wil presumably need.

 
Thanks, I'll check out the Delphi help regarding the TStringList. Even though for purposes of this program, the Array of records structure should do adequately.

But you never know, I might come up with something fishy (and strange ideas must be always implemented!) so I'll definitely check it out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top