2

Is there a way to return multiple values from a function? In a program I'm working on, I wish to return 4 different int variables to the main function, from a separate function, all stats needed to continue through the program. I've found no way to really do this. Any help would be greatly appreciated, thanks.

Jacob Oaks
  • 140
  • 2
  • 11

5 Answers5

8

With C++11 and beyond, you can use std::tuple and std::tie to do programming that is very much in the style of languages like Python's ability to return multiple values. For example:

#include <iostream>
#include <tuple>

std::tuple<int, int, int, int> bar() {
    return std::make_tuple(1,2,3,4);
}

int main() {

    int a, b, c, d;

    std::tie(a, b, c, d) = bar();

    std::cout << "[" << a << ", " << b << ", " << c << ", " << d << "]" << std::endl;

    return 0;
}

And if you have C++14, this becomes even cleaner since you don't need to declare the return type of bar:

auto bar() {
    return std::make_tuple(1,2,3,4);
}
sfjac
  • 7,119
  • 5
  • 45
  • 69
7

C++ doesn't support returning multiple values, but you can return single values of types which contain instances of other types. For example,

struct foo
{
  int a, b, c, d;
};

foo bar() {
  return foo{1, 2, 3, 4};
}

Or

std::tuple<int, int, int, int> bar() {
  return std::make_tuple(1,2,3,4);
}

Or, in C++17, you will be able to use structured bindings, which allow you to initialize multiple objects from functions returning types representing multiple values:

// C++17 proposal: structured bindings
auto [a, b, c, d] = bar(); // a, b, c, d are int in this example
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • okay, so then how would I save those 4 return values as a variable back in main? Would they just automatically overwrite what they were before, or will I have to manually make them do that somehow? Sorry, I'm still very new with C++. – Jacob Oaks Apr 19 '15 at 14:20
  • 1
    Pretty sure that's why I'm here, but thanks anyways. – Jacob Oaks Apr 19 '15 at 14:23
  • 1
    See my answer on how to use `std::tie` on the return side. – sfjac Apr 19 '15 at 14:52
4

One solution could be to return a vector from your function :

std::vector<int> myFunction()
{
   std::vector<int> myVector;
   ...
   return myVector;
}

Another solution would be to add out parameters :

int myFunction(int *p_returnValue1, int *p_returnValue2, int *p_returnValue3)
{
   *p_var1 = ...;
   *p_var2 = ...;
   *p_var3 = ...;
   return ...;
}

In the second example, you'll want to declare the four variables that will contain the four results of your code.

int value1, value2, value3, value4;

After that, you call your function, passing the address of each of your variables as parameters.

value4 = myFunction(&value1, &value2, &value3);

EDIT : This question has been asked before, flagging this as duplicate. Returning multiple values from a C++ function

EDIT #2 : I see multiple answers suggesting a struct, but I don't see why "declaring a struct for a single function" is relevant when their are obviously other patterns like out parameters that are meant for problems like this.

Community
  • 1
  • 1
Corb3nik
  • 1,177
  • 7
  • 26
2

If the type of all variables you wish top return is the same, you can just return an array of them:

std::array<int, 4> fun() {
    std::array<int,4> ret;
    ret[0] = valueForFirstInt;
    // Same for the other three
    return ret;
}
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
1

You can use a function which takes 4 ints as references.

void foo(int& a, int& b, int& c, int& d)
{
    // Do something with the ints
    return;
}

Then use it like

int a, b, c, d;
foo(a, b, c, d);
// Do something now that a, b, c, d have the values you want

However, for this particular case (4 ints) I would recommend @juanchopanza's answer (std::tuple). I added this approach for completeness.

Kvothe
  • 1,819
  • 2
  • 23
  • 37