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?
Try going to Reference / Containers / <vector> trough left sidebar on this page, that might help you.
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <vector>

std::vector<int> return_three()
{
    std::vector<int> r;

    r.push_back(1);
    r.push_back(2);
    r.push_back(3);
    return r;
}

int main()
{
    std::vector<int> vi = return_three();

    std::clog << vi[0] << ' ' << vi[1] << ' ' << vi[2] << '\n';
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <functional>
#include <iostream>
 
std::tuple<int, int, int > f()
{
        return ( std::make_tuple( 10, 20, 30 ) );
}
 
int main()
{
    std::tuple<int, int, int> 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <functional>
#include <iostream>
 
std::tuple<int, int, int> f( int x )
{
        return ( std::make_tuple( x, x * x, x * x * x ) );
}
 
int main()
{
    std::tuple<int, int, int> 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <array>
 
std::array<int, 3> f( int x )
{
        return ( std::array<int, 3> { x, x * x, x * x * x } );
}
 
int main()
{
    std::array<int, 3> 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
 
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;
}
@SamuelAdams

Just declare global


It is a bad idea.
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.