How to return multiple integers from a function?

Apr 26, 2013 at 12:39pm
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!
Apr 26, 2013 at 12:40pm
You could return a vector containing all the integers.
Apr 26, 2013 at 12:55pm
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 Apr 26, 2013 at 12:55pm
Apr 26, 2013 at 12:55pm
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.
Apr 26, 2013 at 12:59pm
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 Apr 26, 2013 at 12:59pm
Apr 26, 2013 at 12:59pm
thanks all of you for immediate replys.
But i have no concept towards vectors can any one give me a good link?
Apr 26, 2013 at 1:03pm
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 Apr 26, 2013 at 1:07pm
Apr 26, 2013 at 1:09pm
Apr 26, 2013 at 1:19pm
ok! i have a user defined funtion and it should return 3 int type values. How can i use vectors to return them?
Apr 26, 2013 at 1:33pm
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';
}
Apr 26, 2013 at 1:40pm
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 Apr 26, 2013 at 1:45pm
Apr 26, 2013 at 1:52pm
Just declare global
Apr 26, 2013 at 1:53pm
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
Apr 26, 2013 at 2:09pm
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;
}
Apr 26, 2013 at 2:23pm
@SamuelAdams

Just declare global


It is a bad idea.
Apr 26, 2013 at 2:58pm
I'd recommend Peter87's approach. Don't over complicate it.
May 1, 2013 at 6:26am
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
May 1, 2013 at 6:27am
thanks all of you problem has been solved.
May 1, 2013 at 6:32am
+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?
May 1, 2013 at 6:46am
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 May 1, 2013 at 6:48am
Topic archived. No new replies allowed.