# Structure and function in C programming

In this tutorial, we will learn how to pass structures as argument to a function and how to manipulate the structure.

Functions and structure together helps to make the program in Object Oriented.

Example : object-oriented program design. Consider a structurethat represents a complex number type.

```
typedef struct {
double real;
double imag;
} Complex;
```

Construct a Complex object with the initial values r and i

```
Complex make complex(double r, double i)
{
Complex c;
c.real = r;
c.imag = i;
return c;
}
```

```
Complex add_complex(Complex a, Complex b)
{
Complex sum;
sum.real = a.real + b.real;
sum.imag = a.imag + b.imag; //Add two complex numbers
return sum; //return the result
}
Complex mult_complex(Complex a, Complex b)
/* Multiply two complex numbers and return the result. */
{
Complex prod;
prod.real = a.real*b.real − a.imag*b.imag;
prod.imag = a.real*b.imag + a.imag*b.real;//Multiply two complex numbers
return prod; //returns the result
}
void addequal_complex(Complex* a, const Complex* b)
{
a−>real += b−>real;
a−>imag += b−>imag; //Add two complex numbers and store the result in a
}
int is_equal(Complex a, Complex b)
{
return a.real == b.real && a.imag == b.imag; //Return TRUE if the values of a and b are equal
}
```

In above example, functions **add_complex(**Complex a, Complex b**)** takes two arguments as input.

```
Complex sum;
sum.real = a.real + b.real;
```

Here, **sum** is the variable of type **complex** . **a.real ** access the real variable of **complex a** and **b.real** access the **real** variable of **complex b.** Similarly for the **imag** variable.

As mentioned in previous topic we access variable of structure using ( . ).

But in above example, in **addequal_complex** function **->** is used. **->** is used to access structure pointer member.

`a−>real += b−>real;`

Here, (*a).real is same as a->real and

(*b).real is same as b->real