# Return 2 variables?

This is a discussion on Return 2 variables? within the C++ Programming forums, part of the General Programming Boards category; Is it possible to return 2 variables for ex. Code: int hello(int x, int y) { x++; y++; return x,y; ...

1. ## Return 2 variables?

Is it possible to return 2 variables for ex.
Code:
```int hello(int x, int y)
{
x++;
y++;
return x,y;
}```

2. Example:
Code:
```void hello(int &x, int &y) {
x++;
y++;
}```

3. 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);
}```

4. how would i recieve it?
Code:
`x = return2numbers();`
xbeing a struct?

5. From Thantos' example:
Code:
`Return2Ints someStruct = hello (1,2);`

To get to the actual values
Code:
```int a = someStruct.x;
int b = someStruct.y;```

7. 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.

8. >> 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

9. 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;
}```

10. 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.