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: stackoverflow.com/questions/1468375/…P Shved– P Shved2010-04-03 16:52:34 +00:00Commented Apr 3, 2010 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– pocoa2010-04-04 02:19:12 +00:00Commented Apr 4, 2010 at 2: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 doubles 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 Comment
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? 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?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; } 4 Comments
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.
1 Comment
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.
Comments
#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); } Comments
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,
1 Comment
c1's lifetime ends when aa is left, so c is left pointing at a dead object. Accessing it leads to undefined behavior.