Abstract
To return a value to function compute, you can use return. What if you want to restore more than two values?
Introduction
There are still many methods for returning more than two values to function compute. The simplest method is to use pass by address/pass by reference.
A very simple requirement. After adding two integers, you want to return the result of adding and multiplying the values at the same time.
C. Yan
1 /*
2 (C) oomusou 2008 Http://oomusou.cnblogs.com
3
4 Filename: fuction_return_two_value.c
5 Compiler: Visual C + + 8.0
6 Description: Demo how to return 2 value by C
7 Release: 03/15/2008 1.0
8 */
9
10 # Include < Stdio. h >
11
12 Void Func ( Int X, Int Y, Int * Sum, Int * Mul ){
13 * Sum = X + Y;
14 * Mul = X * Y;
15 }
16
17 Int Main (){
18 Int X = 2 ;
19 Int Y = 3 ;
20 Int SUM;
21 Int Mul;
22
23 Func (x, y, & Sum, & Mul );
24
25 Printf ( " Sum = % d \ n " , Sum );
26 Printf ( " Mul = % d \ n " , Mul );
27 }
Results
Sum = 5
Mul = 6
Because return can only return a single value, return cannot be used, but more than two values can be returned if two pass by addresses are used.
In C ++, it is a little different. Because c ++ proposes the reference definition, you do not need to use pointer.
C ++
1 /*
2 (C) oomusou 2008 Http://oomusou.cnblogs.com
3
4 Filename: fuction_return_two_value.cpp
5 Compiler: Visual C + + 8.0
6 Description: Demo how to return 2 value by C ++
7 Release: 03/15/2008 1.0
8 */
9
10 # Include < Iostream >
11
12 Using Namespace STD;
13
14 Void Func ( Int Const X, Int Const Y, Int & Sum, Int & Mul ){
15 Sum = X + Y;
16 Mul = X * Y;
17 }
18
19 Int Main (){
20 Int X = 2 ;
21 Int Y = 3 ;
22 Int SUM;
23 Int Mul;
24
25 Func (X, Y, sum, mul );
26
27 Cout < " Sum = " < Sum < Endl;
28 Cout < " Mul = " < Mul < Endl;
29 }
ResultsSum= 5
Mul= 6
C ++ does not use pointer, and the program looks better than C. The reference is mainly implemented by C ++ for pointer.
See also
(Formerly called callback) How to Make function callback return multiple values? (Using iterator) (C #) (. NET)
Conclusion
If you want to return more values, we recommend that you do not use this method. It is better to return a sturct record.