32

I would like to return two double variables: when calling a function that I have created. According to some tutorials (which deal with the basics of the C++), I would be unable to do that.

Is there a way to do so?

Zeus M
  • 407
  • 2
  • 7
  • 13
  • 1
    See http://stackoverflow.com/q/321068/10077 – Fred Larson Mar 12 '13 at 15:55
  • Just in case that someone (like me!) forget how to do it in C, the trick is to pass a pointer to the function and "return" void. As an example, a function that receives two double (x,y) and return the vector divided by the norm (x/d,y/d), where d = sqrt(x²+y²): `void normalized(double x1, double x2, double * vector){ double norm; norm = sqrt(x1*x1+x2*x2); vector[1] = x1/norm; vector[2] = x2/norm; }` – MCL Dec 03 '19 at 16:41

8 Answers8

51

You could write a simple struct that holds the variables and return it, or use an std::pair or std::tuple:

#include <utility>

std::pair<double, double> foo()
{
  return std::make_pair(42., 3.14);
}

#include <iostream>
#include <tuple> // C++11, for std::tie
int main()
{
  std::pair<double, double> p = foo();
  std::cout << p.first << ", " << p.second << std::endl;

  // C++11: use std::tie to unpack into pre-existing variables
  double x, y;
  std::tie(x,y) = foo();
  std::cout << x << ", " << y << std::endl;

  // C++17: structured bindings
  auto [xx, yy] = foo(); // xx, yy are double
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • 3
    Also might want to mention [`std::tie`](http://en.cppreference.com/w/cpp/utility/tuple/tie), which can be used to capture the return in two separate, pre-existing variables. – Benjamin Lindley Mar 12 '13 at 16:04
  • @BenjaminLindley Good suggestion. Added an example. – juanchopanza Mar 12 '13 at 16:13
  • 3
    Also now you may do `return { 42., 3.14 };` which is even prettier and faster (avoids the construction of a pair if you have a `tie(x,y)` on the other side – WurmD Mar 12 '19 at 10:45
20

If you're using C++11, I'd say the ideal way is to use std::tuple and std::tie.

Example taken from the std::tuple page I linked to:

#include <tuple>
#include <iostream>
#include <string>
#include <stdexcept>

std::tuple<double, char, std::string> get_student(int id)
{
    if (id == 0) return std::make_tuple(3.8, 'A', "Lisa Simpson");
    if (id == 1) return std::make_tuple(2.9, 'C', "Milhouse Van Houten");
    if (id == 2) return std::make_tuple(1.7, 'D', "Ralph Wiggum");
    throw std::invalid_argument("id");
}

int main()
{
    auto student0 = get_student(0);
    std::cout << "ID: 0, "
              << "GPA: " << std::get<0>(student0) << ", "
              << "grade: " << std::get<1>(student0) << ", "
              << "name: " << std::get<2>(student0) << '\n';

    double gpa1;
    char grade1;
    std::string name1;
    std::tie(gpa1, grade1, name1) = get_student(1);
    std::cout << "ID: 1, "
              << "GPA: " << gpa1 << ", "
              << "grade: " << grade1 << ", "
              << "name: " << name1 << '\n';
}
Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
19

You can pass references to two doubles into a function, setting their values inside the function

void setTwoDoubles(double& d1, double& d2)
{
    d1 = 1.0;
    d2 = 2.0;
}

double d1, d2;
setTwoDoubles(d1, d2);
std::cout << "d1=" << d1 << ", d2=" << d2 << std::endl
simonc
  • 41,632
  • 12
  • 85
  • 103
7

You can use a std::pair, for instance.

unwind
  • 391,730
  • 64
  • 469
  • 606
4

Technically, no you are not able to return two variables in the way you would normally return a variable. You can, however, use references. That way, you can pass multiple variables to a function, and the function will assign them, rather than returning anything:

void function(double & param1, double & param2) {
    param1 = 6.28;
    param2 = 3.14;
}

And you would call it like this:

double var1, var2;
function(var1, var2);
antonijn
  • 5,702
  • 2
  • 26
  • 33
2

You can't do it directly (because a return value is singular).
But, you could put a few values in a structure, and return that (like a pair<>).

A common pattern is to return output variables by reference:

ReturnVal Myfunction(/*in*/ BlahType _someParameters, /*out*/ ReturnType& _firstReturn, /*out*/ OtherReturnType& _secondReturn)
{
   _firstReturn = //someStuff
   _secondReturn = //someOtherStuff


return SUCCESS;
}
Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
2

No you can not return two variables you need to use by reference method as

    #include <iostream>
using namespace std;

// function declaration
void swap(int &x, int &y);

int main ()
{
   // local variable declaration:
   int a = 100;
   int b = 200;

   cout << "Before swap, value of a :" << a << endl;
   cout << "Before swap, value of b :" << b << endl;

   /* calling a function to swap the values using variable reference.*/
   swap(a, b);

   cout << "After swap, value of a :" << a << endl;
   cout << "After swap, value of b :" << b << endl;

   return 0;
}


// function definition to swap the values.
void swap(int &x, int &y)
{
   int temp;
   temp = x; /* save the value at address x */
   x = y;    /* put y into x */
   y = temp; /* put x into y */

   return;
}

the output will be 100 // x before calling swap function 200 // y before calling swap function 200 // x after calling swap function 100 // y after calling swap function

this as return two values

this link help you

Maadh
  • 643
  • 4
  • 24
  • Hi, I think this answer needs to be modified due to the new features of C++ 11 (and above) like `std::pair`. – WY Hsu Feb 04 '19 at 15:09
0

You cannot return two values from one function, but you could return a pointer to an array or some other structure which contains the two doubles.

RouteMapper
  • 2,484
  • 1
  • 26
  • 45