GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Sunday, December 19, 2010

C++ Function Return By Reference

If you want to return more than just one return value, use function reference.
"&" is an address of. Use this sign for declaration and definition.

#include <iostream>

using namespace std;

void triangle(int, int, int&, int&);

int main(){
   int base = 6;
   int height = 4;
   int area;
   int circumference;

   triangle(base, height, area, circumference);

   cout << "Triangle arithmatics" << endl;
   cout << "base: " << base << " & height: " << height << endl;
   cout << "Area: " << area << endl;
   cout << "Circumference: " << circumference << endl;

   return 0;
}

void triangle(int base, int height, int& area,
              int& circumference){
    area = 0.5 * base * height;
    circumference = base + (2 * height);
}
Share/Bookmark

No comments:

Post a Comment