Mega Code Archive

 
Categories / C++ Tutorial / Structure
 

Return structure from a function

#include <cmath> #include <iostream> using namespace std; // define the structures struct DataStructure {   float radius;   double angle; }; struct ResultStructure {   float area;   double sine;   double cosine;   double tangent; }; ResultStructure compute(struct DataStructure mystruct); int main () {     DataStructure input;        ResultStructure output;        input.radius = 3;        input.angle = 0.8;        output = compute(input);     cout << " The area is "<< output.area << "\n";        cout << " The sine of the angle is " << output.sine << "\n";        cout << " The cosine of the angle is " << output.cosine << "\n";     cout << " The tangent of the angle is " << output.tangent << "\n";        return 0; } ResultStructure compute(struct DataStructure mystruct) {      ResultStructure answer;         answer.area = pow(mystruct.radius,2);         answer.sine = sin(mystruct.angle);         answer.cosine = cos(mystruct.angle);         answer.tangent = tan(mystruct.angle);         return answer; }; The area is 9 The sine of the angle is 0.717356 The cosine of the angle is 0.696707 The tangent of the angle is 1.02964