Functions that returns two int variables or two char variables.

closed account (iAk3T05o)
Is it possible to have a function (non-void) that returns two variables i.e.
1
2
3
4
5
int blah(int a, int b)
{
//code
return a, b;
}

not return a+b/a*b etc.
Yup:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct TWO_NUMBERS
{
     int a;
     int b;
};

TWO_NUMBERS blah(int a, int b)
{
     TWO_NUMBERS TwoNumbers;
     TwoNumbers.a = a;
     TwoNumbers.b = b;

     return TwoNumbers;
}
I believe c++11 allows you the ability to return tuples. So you should check that out

1
2
3
4
5
6
7
#include <tuple>

std::tuple<int, int> blah(int a, int b)
{
    // some code
    return std::make_tuple(a, b);
}


Of course you can decide to go with std::pair as suggested, but if you want to return more than 2 items, c++11 has variadic templates, tuples, initializer_list and of course tuple unpacking. So you can read up on those if you are interested
Last edited on
No, but you could use a type that contains more than one data member.
1
2
3
4
std::pair<int, int> blah(int a, int b)
{
	return {a, b};
}
Of course make sure you understand passing variables by reference before you start returning structs from every function, just to make sure you aren't asking because of that.
closed account (iAk3T05o)
Well i haven't learnt structs/pair/tuple.
This clears up a lot because i always tried to do it and failed and it made me think i didn't know how to use functions.
Finally, i can move on in my tutorial . . . i think.
closed account (iAk3T05o)
@james: what do you mean by reference?
Look up "Passing by value vs Passing by reference" and you will see many useful topics explaining why it is important. Basically you pass variables by member address so the original variable gets updated inside the function and a copy does not have to be made/you don't have to try and return things. In this case it would be
int blah(int &a, int &b)

http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/
http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
The goal is to generate the result without modifying the original parameters.

Using designated "output" parameters is a bad habit that should be broken quickly - it is a C-like thing to do and will result in poor designs.
closed account (iAk3T05o)
@L B: What are you refering to?
@Nathan: I am referring to @James' post.
closed account (iAk3T05o)
So it's better to pass by reference?
I wouldn't say either one is "better" than the other, it depends on what you are trying to accomplish.

As LB said if you are creating a completely new variable based on the parameters it is more likely you will want to return it from that function. If you just want to modify some of the values that you passed in to the function you would pass by reference instead of returning a value (In general).

@LB I know specific output variables aren't the best idea I just wanted to make sure OP knew passing by reference and wasn't trying to learn returning multiple values for the wrong reasons.
Last edited on
Nathan2222 wrote:
So it's better to pass by reference?
No - please read:
http://www.cplusplus.com/articles/z6vU7k9E/
It is entirely situational.
Last edited on
Topic archived. No new replies allowed.