Pass structure to a function : structure parameter « Structure « C++ Tutorial






#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








8.3.structure parameter
8.3.1.Pass structure to a function
8.3.2.Pass structure using a structure reference
8.3.3.Pass structure to a function as a pointer
8.3.4.structure variables as array elements