Can a function return more than one value directly (i.e., without returning in parameters taken by-reference)?
-
1In case you want a language-agnostic overview, here's a question: http://stackoverflow.com/questions/1468375/how-do-you-return-two-values-from-a-single-method – P Shved Apr 03 '10 at 16:52
-
You can return as an array or you can pass an array as a reference and store these values into that array. – pocoa Apr 04 '10 at 02:19
7 Answers
In the boost::tuple
library, there's a function called tie
that simplifies the process of getting information out of a returned tuple
. If you had a function that returned a tuple
of two double
s and wanted to load those into two local variables x
and y
, you could assign your function's return value to boost::tie(x, y)
.
Example:
#include <math.h>
#include <iostream>
#include <boost/tuple/tuple.hpp>
const double PI = 3.14159265;
boost::tuple<double, double> polar_to_rectangular(double radius, double angle)
{
return boost::make_tuple(radius * cos(angle), radius * sin(angle));
}
int main()
{
double x;
double y;
boost::tie(x, y) = polar_to_rectangular(4, (45 * PI) / 180);
std::cout << "x == " << x << ", y == " << y << std::endl;
return 0;
}

- 1,338
- 1
- 10
- 17
-
When I tried to use `boost::tuple`, it gave me error `fatal error: boost/tuple/tuple.hpp: No such file or directory`, it's not part of some [standard library](https://en.cppreference.com/w/cpp/header)? I am running my program on ubuntu16.04 with command `g++ -std=c++11 swap.cpp -o main`. if I don't use `boost::tuple`, my program runs fine! Any suggestions what I am doing wrong? – Anu Jan 14 '19 at 22:24
Yes - have your function return a struct. Or return the values via reference parameters.
struct A {
int x, y;
A(int x, int y) : x(x), y(y) {}
};
A myfun() {
return A(0, 42); // return two values
}
or:
void myfun(int & a, int & b) {
a = 0;
b = 42;
}

- 3
- 1
- 3
-
4
-
Is this answering the question, "Can a function modify more than one value?" It can also modify globals if you want to be complete. – Hogan Apr 03 '10 at 16:25
-
but still only one value(its address) has been sent and we can access the whole object by copying it into another object. i want to know specifically whether we can assign two values simultaneously. – Ashish Yadav Apr 03 '10 at 16:31
-
@ashish No addresses involved. But to answer your second question, no you can't. – Apr 03 '10 at 16:35
No, but you can return a pair
or boost::tuple
which can contain multiple values.
In addition, you can use references to return multiple values like this:
void MyFunction(int a, int b, int& sum, int& difference);
You would call this function like this:
int result_sum;
int result_difference;
MyFunction(1, 2, result_sum, result_difference);
As Hogan points out, technically this isn't returning multiple variables, however it is a good substitute.

- 65,341
- 56
- 178
- 228
-
9Nit pick: "In addition, you can return multiple values like this:" should be "In addition, you can modify multiple values like this:". You can also modify globally scoped variables. – Hogan Apr 03 '10 at 16:26
A function can return values in the specified ways:
- Via return value of any type
- Via a pointer
- Via a reference
- Via setting a global variable (highly not recommended)
If you need a self contained return value, you would typically wrap the types you need in a struct and return an object of that struct by value. If you want to avoid keeping a local copy you would pass in a reference parameter to be modified.

- 339,232
- 124
- 596
- 636
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
int a;
int b;
}Mystruct;
Mystruct myfun();
int main()
{
char name[30];
Mystruct ms2;
ms2 = myfun();
printf("val1: %d val2: %d",ms2.a,ms2.b);
return 0;
}
Mystruct myfun()
{
int a,b;
Mystruct ms;
a = 10;
b = 20;
ms.a=a;
ms.b=b;
return(ms);
}

- 90,663
- 31
- 146
- 203

- 31
- 1
main()
{
int a=10,b=20;
int *c;
c=aa(a,b);
printf("%d %d",*c,*c+1);
}
void aa(int a,int b)
{
int c1[2];
c1[0]=b+a;
c1[1]=a-b;
return(c1);
}
here, the address of c1 will be return. so it will store in main c cariable. we can retrive both variable via pointer,

- 90,663
- 31
- 146
- 203
-
2Leads to undefined behavior. `c1`'s lifetime ends when `aa` is left, so `c` is left pointing at a dead object. Accessing it leads to undefined behavior. – GManNickG Aug 18 '10 at 08:07