Consider these two functions: power2()
and add()
:
void power2 (double& res, double x) {
res = x * x;
}
double& add (double& x) {
return ++x;
}
The first computes the power of x
and stores the result in the first argument, res
, – it does not need to return it.
The second returns a reference, which means this reference can later be assigned a new value.
Example:
double res = 0;
power2(res, 5);
printf("%f\n", res);
printf("%f\n", ++add(res));
Output:
25.000000
27.000000
Please note that the second output is 27
, not 26 – it's because of the use of ++
inside the printf()
call.