How to return multiple integers from a function?

How to return multiple integers from a function?
Hello every one can you tell me:

How can i have multiple ways to return multiple values mostly integers
from an funtion? I needed in my project. Help!
You could return a vector containing all the integers.
As you CANNOT return arrays. As Peter states, you could try with vectors.

I also believe it is legit to do something like the following:

`return num1, num2, num3`
Last edited on
 multiple values mostly integers
That implies some values are not integers.

Depending on the requirements, you might use a user-defined structure containing whatever types are required. Or pass parameters by reference.
 I also believe it is legit to do something like the following: `return num1, num2, num3`
That will only return num3 (unless you have overloaded operator,).
`return num3;`
Last edited on
thanks all of you for immediate replys.
But i have no concept towards vectors can any one give me a good link?
http://www.cplusplus.com/reference/vector/vector/

Although, considering you have various types your function needs to return I would suggest user defined type to be stored in the vector you return from function.
Last edited on
ok! i have a user defined funtion and it should return 3 int type values. How can i use vectors to return them?
 ``12345678910111213141516171819`` ``````#include #include std::vector return_three() { std::vector r; r.push_back(1); r.push_back(2); r.push_back(3); return r; } int main() { std::vector vi = return_three(); std::clog << vi[0] << ' ' << vi[1] << ' ' << vi[2] << '\n'; }``````
 ``12345678910111213141516`` ``````#include #include std::tuple f() { return ( std::make_tuple( 10, 20, 30 ) ); } int main() { std::tuple myInt = f(); std::cout << std::get<0>( myInt ) << ", " << std::get<1>( myInt ) << ", " << std::get<2>( myInt ) << std::endl; }``````

To make the example more interesting I added a parameter to function f.

 ``12345678910111213141516`` ``````#include #include std::tuple f( int x ) { return ( std::make_tuple( x, x * x, x * x * x ) ); } int main() { std::tuple myInt = f( 10 ); std::cout << std::get<0>( myInt ) << ", " << std::get<1>( myInt ) << ", " << std::get<2>( myInt ) << std::endl; }``````

Th output is

 `10, 100, 1000`
Last edited on
Just declare global
The other simple way is to use std::array instead of std::tuple. For example

 ``12345678910111213141516`` ``````#include #include std::array f( int x ) { return ( std::array { x, x * x, x * x * x } ); } int main() { std::array myInt = f( 10 ); std::cout << myInt[0] << ", " << myInt[1] << ", " << myInt[2] << std::endl; }``````

The output is

 10, 100, 1000
And to show how you can do it using references.
 ``12345678910111213141516171819`` ``````#include void f(int& x, int& y, int& z) { x = 1; y = 2; z = 3; } int main() { int a; int b; int c; f(a, b, c); std::cout << a << ' ' << b << ' ' << c << std::endl; }``````

I'd recommend Peter87's approach. Don't over complicate it.
i can't use refernce in my program cox i have to pass a 2nd outer function through 1st outer funtion than i will call 1st outer funtion in main funtion peter87's approach isn't helpful. moorecm and peter87
thanks all of you problem has been solved.
+1 to Peter87.
-1 to those suggesting globals or tuples.

Why would you suggest a tuple to somebody who has never heard about pass by reference or vectors?
Stewbond wrote:
+1 to Peter87.
-1 to those suggesting globals or tuples.

Why would you suggest a tuple to somebody who has never heard about pass by reference or vectors?

Why not? (One shouldn't forget persons other than the OP may glean information from this thread in the future.)

What score do you give people who did nothing but rate other people in this thread?

On topic: Speaking of tuples and ever more options, you could go the simple route and just define a type (read struct) which could hold all of the values you want to return and return that.
Last edited on
Topic archived. No new replies allowed.