Can A Function Return More Than Two Values?

Hi..
Remember I did Return Two Values In C,But How Can We Return Two Values In C++? I Googled It. But Couldn't Find A Proper Answer :(



double double findTotal(int);//prototype
main()
{
//how to write in here?
}

double double findTotal(int tcode)
{
double a,b;
//my code
return a,b;
}
In both C and C++ you can only directly return one value.
If you want to return more than one, you must use a struct of some kind.

Here is some fun in C++
http://www.cplusplus.com/forum/beginner/18489/

Good luck!
Thanks :)
Besides using function to return values, you can have a pass-by-pointer or pass-by-reference parameter into the function to make changes on the input variables inside.

Returning a struct out of a function implies more operations needed as a pass-by-value will invoke possibly default constructor,copy constructor,assignment operator which is costly.

If you want efficiency and return a pointer to struct out of a function, you pray the calling program will dispose of that faithfully.

So I would prefer to go back to pass in parameters to function approach.

The pair class may be of use for this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <utility>

std::pair<char, double> funcThatReturnsACharAndDouble()
{
     std::pair<char, double> twoValues;

     twoValues.first = 'c';
     twoValues.second = 2.5;

     return twoValues;
}

int main()
{
     std::pair<char, double> twoValuesFromFunc = funcThatReturnsACharAndDouble();

     std::cout << "< " << twoValuesFromFunc.first << " , " << twoValuesFromFunc.second << " > " << std::endl;

     return 0;
}
WOW Thanks All For Your Help.
Another way.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void f(int& x, int & y)
{
    x = 10;
    y = -10;
}

#include <iostream>

int main()
{
    int a(0);
    int b(0);

    f(a, b);
    std::cout << "a " << a << "\tb " << b << '\n';

    return 0
}
@shacktar, Denis
I always find it impressive how posting a link that covers all these topics proves so useless.
The following was inspired by Duoas' link:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <boost/tuple/tuple.hpp>

#include <iostream>
#include <sstream>
#include <string>

typedef
boost::tuple
<
    std::string,
    unsigned int,
    double,
    double
> MyTupleType;

MyTupleType unpack(const std::string & str)
{
    std::istringstream buffer(str);

    std::string name;
    unsigned int age;
    double weight;
    double height;

    buffer >> name >> age >> weight >> height;

    return boost::make_tuple(name, age, weight, height);
}

int main()
{
    MyTupleType ret = unpack("John 30 90.25 1.75");

    std::cout
        << "name:   " << ret.get<0>() << '\n'
        << "age:    " << ret.get<1>() << '\n'
        << "weight: " << ret.get<2>() << " kg\n"
        << "height: " << ret.get<3>() << " m\n"
        << std::endl;
}
name:   John
age:    30
weight: 90.25 kg
height: 1.75 m

Another useful link -> http://www.boost.org/doc/libs/1_47_0/libs/tuple/doc/tuple_users_guide.html
Last edited on
@Duoas
Your answer does not preclude others from providing more succinct solutions.
sohguanh wrote:
Returning a struct out of a function implies more operations needed as a pass-by-value will invoke possibly default constructor,copy constructor,assignment operator which is costly.

Return Value Optimisation?
You can only return a single value from a function; however, you can return a address in memory that contains a user defined amount of bytes. I encourage you to pass by reference though, for unnecessary memory managment should always be avoided

1
2
3
4
5
6
int *return_array_dyn(void) {
	int *arr = new int[2];
	arr[0] = 1;
	arr[1] = 2;
	return arr;
}
@jloundy That may not be such a great idea. Then it becomes the caller's responsibility to clean up after the callee (using delete). Return by value and let RVO do the rest
http://en.wikipedia.org/wiki/Return_value_optimization
(In other words use something other than an intrinsic array, e.g. std::pair, etc and then return it by value. The compiler is permitted by the C++ standard to remove the copy operation invoked.)
Last edited on
@Xander314, Hence why I encouraged other methods since "unecessary memory managment should always be avoided" ; however, he should know that you can return memory locations.
@shacktar
No, it doesn't, but your answer is no more succinct than many of the answers in the existing thread. You'd know that if you had bothered to read it.
Besides yours, mine was the only one with a code example up to that point. Of course I had read the thread; I felt like it was lacking a succinct code example, though, and judging by the OP's reaction, it was appreciated. And by "many of the answers in the existing thread", you meant the previous two before my post? And if by "existing thread" you mean the one you posted to, then my code example was more easily digestible for beginners like the OP. I (and anyone for that matter) shouldn't have to defend their responses in a thread, even if similar responses were given previously.
Last edited on
Point taken. Sorry.
Thanks All:)
Topic archived. No new replies allowed.