References are a way to implement function's
out-parameters, like pointers did in plain
C
programming.
C++17
offers destructuring in order to avoid them. Try for instance
#include <iostream>
#include <tuple>
#include <cmath>
using namespace std;
void polar( double x, double y, double & rho, double & phi)
{
rho = sqrt(x*x + y*y);
phi = atan2(x,y);
}
void c_style_polar( double x, double y, double * prho, double *pphi)
{
*prho = sqrt(x*x +y*y);
*pphi = atan2(x,y);
}
tuple <double , double> tupled_polar(double x, double y)
{
return make_tuple<double, double> ( sqrt(x*x+y*y), atan2(x,y));
}
int main()
{
double x = 100, y = 100;
{
double rho, phi;
rho = 0.0; phi = 0.0;
polar(x, y, rho, phi);
cout << "rho = " << rho << ", phi = " << phi << '\n';
}
{
double rho, phi;
c_style_polar(x, y, &rho, &phi);
cout << "rho = " << rho << ", phi = " << phi << '\n';
}
{
auto [rho, phi] = tupled_polar(x, y);
cout << "rho = " << rho << ", phi = " << phi << '\n';
}
}