In Delphi, how does function return multiple values?

Source: Internet
Author: User
In Delphi, how does function return multiple values? Delphi/Windows SDK/API
Http://www.delphi2007.net/DelphiBase/html/delphi_20061206210840253.html
I wrote a function myself. If I want to return three double values at the same time, how can I implement it?

Use references as parameters. For example:

Function ABC (var aa, BB, CC: Double): Double; // returns four double values.
Begin
AA: = ......
BB: = ......
Cc: = .....
Result: = ......
End;
//---------------------------------------------
... The following is a call
VaR
Aatrue, bbtrue, cctrue, ddtrue: Double;
Begin
Aatrue: =...
Bbtrue: =...
Cctrue: =...
Ddtrue: = ABC (aatrue, bbtrue, cctrue );
End;
// It is not a case-sensitive mother. It is not easy to read. Sorry.

My test results:

Function ABC (var aa, BB, CC: Double): Double; // returns four double values.
Begin
AA: = 12.6;
BB: = 88.89;
Cc: = 2345.907;
Result: = AA + BB + CC;
End;

Procedure tform1.button2click (Sender: tobject );
VaR
Aatrue, bbtrue, cctrue, ddtrue: Double;
Begin
Aatrue: = 0.0;
Bbtrue: = 0.0;
Cctrue: = 0.0;
Ddtrue: = ABC (aatrue, bbtrue, cctrue );
Memo1.lines. add ('here are the new values of the four variables: '+ floattostr (aatrue) +', '+ floattostr (bbtrue) +', '+ floattostr (cctrue) + ', '+ floattostr (ddtrue ));
End;

Result:
12.6, 88.89, 2345.907, 2447.397

What does result return?
Can I write details? I want to obtain the three return values separately.

What if I didn't pass the parameter? The c_lt_l, c_la_ B, and c_hi_h parameters are all common variables in the class. Now we want to use this function to obtain these three variables.
Function thhucoord. getparams ();
Begin
C_lt_l;
C_la_ B;
C_hi_h;
End;

Either use VaR
Or a record (struct) is returned)

Type iwantdata = record
AA :...;
BB :...;
Cc :...;
End;

Function ABC (var aa, BB, CC: Double): iwantdata; // returns three double values.
VaR
RET: iwantdata;
Begin
Ret. AA: = ......;
Ret. BB: = ......;
Ret. CC: = .....;
Result: = ret;
End;

We strongly despise people who will not close the post after the problem is solved!
People who care about technical issues and transfer their posts to non-technical areas will be despised!
Despise you!

Http://community.csdn.net/Expert/topic/5216/5216675.xml? Temp =. 9262659.

How can a function return multiple values?

To upstairs:

Type
TTwo = record
X: string;
Y: string;
Z: string;
End;

Procedure funone (var a, B, C: integer); // address transfer (reference, alias)
Begin
A: = 9; // assign values to the three variables
B: = 8;
C: = 7;
End;

Function funtwo (instr: string): tTwo; // return record
VaR temprec: tTwo;
Begin
Temp. X: = copy (instr, 1, 1 );
Temp. Y: = copy (instr, 2, 1 );
Temp. Z: = copy (instr, 3, 1 );
Result: = temprec;
End;

Procedure tform1.button1click (Sender: tobject );
VaR
A, B, C: integer;
Instr: string;
Begin
A: = 0; // Initial Value
B: = 0;
C: = 0;
Funone (A, B, C );
// Test the values of A, B, and C processed by funone (equivalent to "return") to 9, 8, and 7.
Showmessage (inttostr (A) + ',' + inttostr (B) + ',' + inttostr (c ));

Instr: = 'xyz ';
// Test the three record Member values x, y, and z returned by funtwo ().
Showmessage (funtwo (instr). x + ',' + funtwo (instr). Y + ',' + funtwo (instr). z );
End;

// In the preceding two methods, if the first method that uses "Reference" for parameters is equivalent to returning multiple values, then the second method that uses records actually returns multiple values.

Change temp. X to temprec. X.
Change temp. Y to temprec. Y.
Change temp. Z to temprec. Z.

VaR xxx; can it be solved?

Thank you.
Indeed, multiple values are modified.

------------------
However, if you drill the horn, it cannot be "returning multiple values,

When calling the data transfer address, it can only be said that three values are changed, and only one variable is returned.
Returns a struct. It is also a variable, but this variable is still a struct.

??
I don't know if dephi or other languages actually return multiple values.

What is the significance of this problem?

Delphi is enough for Nb, and the function can return any type.
You can try C ++. Many types must use pointer pointing to the type to be used as the return value of the function.

Agree with the two layers above

The simplest method is to define several global variables, assign values in functions, and reference them elsewhere. Of course, there are many disadvantages that I don't need to say. I personally don't recommend writing this, but it is really simple.

Upstairs, Do you want others to use your method or not?

Functions and processes are actually the same, and there is no returned value problem!
It is only for the convenience of returning a value.
All are pointers;
Use var!

Why can't I close my post?

Another method can return an array.

Function mytest (...): olevariant;
Begin
Result: = vararraycreate ([0, 2], varinteger );
Result [0]: = 0;
Result [1]: = 1;
Result [2]: = 2;
End;

or define a type
tmytype = array [0 .. 2] of integer;
function mytext (...): tmytype;
begin
result [0]: = 1;
result [1]: = 1;
result [2]: = 0;
end;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.