C++ examples for Function:Function Return
Create function to return larger of two distances
#include <iostream> using namespace std; struct Measure // English distance { int feet;/* w ww . j a v a2 s .c o m*/ float inches; }; Measure bigengl(Measure, Measure); //declarations void engldisp(Measure); int main() { Measure d1, d2, d3; //get length d1 from user cout << "\nEnter feet: "; cin >> d1.feet; cout << "Enter inches: "; cin >> d1.inches; //get length d2 from user cout << "\nEnter feet: "; cin >> d2.feet; cout << "Enter inches: "; cin >> d2.inches; d3 = bigengl(d1, d2); //d3 is larger of d1 and d2 //display all lengths cout << "\nd1="; engldisp(d1); cout << "\nd2="; engldisp(d2); cout << "\nlargest is "; engldisp(d3); cout << endl; return 0; } // compares two structures of type Measure, returns the larger Measure bigengl( Measure dd1, Measure dd2 ) { if(dd1.feet > dd2.feet) //if feet are different, return return dd1; //the one with the largest feet if(dd1.feet < dd2.feet) return dd2; if(dd1.inches > dd2.inches) //if inches are different, return dd1; //return one with largest else //inches, or dd2 if equal return dd2; } // display structure of type Measure in feet and inches void engldisp( Measure dd ) { cout << dd.feet << "\'-" << dd.inches << "\""; }