Returning multiple values from a function using references : reference parameter « Function « C++ Tutorial






#include <iostream>

 void f(int, int&, int&);
 
 int main()
 {
     int number = 2, squared, cubed;

     f(number, squared, cubed);
 
     std::cout << "number: " << number << "\n";
     std::cout << "square: " << squared << "\n";
     std::cout << "cubed: "  << cubed   << "\n";

     return 0;
 }
 
 void f(int n, int &rSquared, int &rCubed)
 {
      rSquared = n*n;
      rCubed = n*n*n;
 }
number: 2
square: 4
cubed: 8








7.4.reference parameter
7.4.1.Using a reference parameter.
7.4.2.Use reference parameters to create the swap() function.
7.4.3.Pass by reference by using pointer
7.4.4.Pass by reference using references
7.4.5.Returning multiple values from a function using references
7.4.6.Passing references to objects