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?
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?
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
}
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';
}
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
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);
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;
}
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
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.