how many return values can a function have in c++?

can i return more than one value from a function in c++? i've tried it myself and it does not work but maybe i am doing it wrong and there is a way to do it?

can i do like

int me()
{
int u = 5;
int tu = 6;

return u, tu


}

?
pass it by reference;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

void foo(int& i, int& k) {
    i = 5;
    k = 6;
}


int main() {
    int me, you;
    foo(me, you);
    cout << me << endl;
    cout << you << endl;
    return 0;
}


EDIT: by the way a function can only return one value
Last edited on
question answered thanks....so i guess there is really no way to return two values at once huh.
so i guess there is really no way to return two values at once huh.

Well... there actually is... consider this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;

struct foo_return_type
{
       int a;
       int b;
};

foo_return_type foo()
{
    foo_return_type fret;
    
    fret.a=12;
    fret.b=34;
    
    return fret;
}

int main()
{
    foo_return_type result;
    
    result=foo();
    
    cout << result.a << endl;
    cout << result.b << endl;
    
    cin.get();
    return 0;
}

But if what you want to return is big in size, it would be better to pass it as an argument by reference (as blackcoder41 also suggests) like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std;

struct foo_return_type
{
       int a;
       int b;
       int c[10];
};

void foo(foo_return_type & fret)
{
    fret.a=12;
    fret.b=34;
    
    for (int i=0; i<10; i++)
        fret.c[i]=i+1;
    
    return;
}

int main()
{
    foo_return_type result;
    
    foo(result);
    
    cout << result.a << endl;
    cout << result.b << endl;
    
    for (int i=0; i<10; i++)
        cout << result.c[i] << ' ';
    
    cout << endl;
    
    cin.get();
    return 0;
}
Last edited on
it can only still return one foo_return_type
it can only still return one foo_return_type
Yeap! But two values are returned ;)
Last edited on
To sum up, a good way to pass a bunch of arguments to a function and get another bunch of returned values would be the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;

struct foo_argument
{
       //put the definitions u want here
       //...
};

struct foo_return_value
{
       //put the definitions u want here
       //...
};

void foo(foo_argument & farg, foo_return_value & fret)
{
    //do what you want here
    //...
}

int main()
{
    foo_argument foo_in;
    foo_return_value foo_out;
    
    //set the arguments
    //...
    
    //call foo
    foo(foo_in,foo_out);

    //do what you want with the returned values
    //...

    return 0;
}
Similar to C , in c++ also u can return only one value
Topic archived. No new replies allowed.