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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can a function return two values?

Status
Not open for further replies.

CADTenchy

Technical User
Dec 12, 2007
237
GB

I need a function to return two values (floats) having been passed a string.

Is this possible, as the editor doesn't like it and google doesn't come up with much?

Steve (Delphi 2007 & XP)
 
Usually the rule of a function is to return one discrete thing. You aren't going to find much on that regard because if you want to return multiple things, it is preferred to return them in the variable listings rather than the result.

BUT (untested)...

Code:
{$APPTYPE CONSOLE}
program mytest;
type
  myrec = record
    float1: extended;
    float2: extended;
  end;

function myfunc: myrec;
  begin
    MyFunc.Float1 := 0.12;
    MyFunc.Float2 := 0.24;
  end;

var
  mydata: myrec;
begin
  Mydata := MyFunc;
  writeln(Mydata.Float1);
  writeln(Mydata.Float2);
end.

Though it's highly preferable that you do not...

----------
Measurement is not management.
 
thanks Glenn. I have been passing them to variables so far.
Would have been 'nice' as the results are Lat & Long, which always need to be a pair.

I'll stick with variables then, but out of interest why do you say your offering is highly undesirable to do?

Steve (Delphi 2007 & XP)
 
> but out of interest why do you say your offering is highly undesirable to do?

Two things: overhead, and common idioms.

There is a certain amount of overhead (both in code listings and in run-time memory use -- though not really in this case) to doing it that way.

But the other is just that it is not a common idiom for Delphi developers and is more likely to cause confusion than just
Code:
procedure myproc( out f: extended; out n: cardinal );
  ...

var
  float1: extended;
  number: cardinal;
begin
  myproc( float1, number );
  ...

Hope this helps.
 
There's nothing stopping you pairing them up when it comes to procedures like I showed in the example above. The idea is that people generally assume functions to return one and only one discrete value. So it's very bad form.

----------
Measurement is not management.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top