15

Can a function return more than one value directly (i.e., without returning in parameters taken by-reference)?

2
  • 1
    In case you want a language-agnostic overview, here's a question: stackoverflow.com/questions/1468375/… Commented 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. Commented Apr 4, 2010 at 2:19

7 Answers 7

24

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; } 
Sign up to request clarification or add additional context in comments.

1 Comment

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? 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?
15

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

Though technically that's still one value in C
Is this answering the question, "Can a function modify more than one value?" It can also modify globals if you want to be complete.
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 No addresses involved. But to answer your second question, no you can't.
13

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

Nit 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.
1

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

1
#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

0

use structure and return multiple value with different data type.

Comments

-4
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

Leads 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.