# Return 2 variables?

• 01-11-2005
ay_okay
Return 2 variables?
Is it possible to return 2 variables for ex.
Code:

```int hello(int x, int y) {   x++;   y++;   return x,y; }```
• 01-11-2005
master5001
Example:
Code:

```void hello(int &x, int &y) {   x++;   y++; }```
• 01-11-2005
Thantos
No. But you can wrap them up in a struct and then return the struct.

Code:

```struct Return2Ints {   int x;   int y; }; Return2Ints hello (int x, int y) {   return Return2Ints(x+1,y+1); }```
• 01-11-2005
ay_okay
how would i recieve it?
Code:

`x = return2numbers();`
xbeing a struct?
• 01-11-2005
bithub
From Thantos' example:
Code:

`Return2Ints someStruct = hello (1,2);`
• 01-11-2005
Thantos
To get to the actual values
Code:

```int a = someStruct.x; int b = someStruct.y;```
• 01-11-2005
azteched
A generic way to do this is to return an std::pair:

std::pair<int,int> hello(int x, int y); { return std::make_pair (x,y); }

std::pair bla = hello(1,2);
cout << bla.first; //x

Tuples (look up Boost.Tuple) let you return any number of values. You can use Boost.Tuple in a similar way to std::pair.
• 01-11-2005
Codeplug
>> return Return2Ints(x+1,y+1);
Return2Ints needs a proper constructor for that to work.
Code:

```struct Return2Ints {   int x;   int y;   Return2Ints(int x_, int y_) : x(x_), y(y_) {} };```
gg
• 01-11-2005
Thantos
Opps forgot about that. Though it can be done without a constructor:
Code:

```Return2Ints hello(int x, int y) {   Return2Ints ret = {x+1,y+1};   return ret; }```
• 01-12-2005
hk_mp5kpdw
Quote:

Originally Posted by azteched
A generic way to do this is to return an std::pair:

std::pair<int,int> hello(int x, int y); { return std::make_pair (x,y); }

std::pair<int,int> bla = hello(1,2);
cout << bla.first; //x

Tuples (look up Boost.Tuple) let you return any number of values. You can use Boost.Tuple in a similar way to std::pair.