Structures and Functions

Structures and Functions

In this article, we are going to discuss Structures and Functions. Please read our previous article, where we discussed How to pass a Structure as a Parameter. This is an important topic. The style of code that we have written here, the same style coding is followed in this course. You should be familiar with this structure or with this style of programming.

Structures and Functions:

Let us understand the coding standard that we are going to follow with an example. Let us say, we have a structure called Rectangle with two data members length and breadth as shown below.

struct Rectangle
{
    int length;
    int breadth;
};

Now we want to create a rectangle object, initialize it, then we need to calculate the area, and then we need to change the length of the rectangle. We can do everything within the main function itself, but that is not a good programming approach. For each operation, we will create a separate function. So, the most important point to observe here is the main function is not having any instructions of its own but only the calling functions. Everything is done by the calling functions. There is a function for initialization, there is a function for calculating the area, and another function for changing the length of the rectangle. So, the main should look as shown below.

int main ()
{
    struct Rectangle r;
    Initialize (&r, 10, 5);
    Area (r);
    ChangeLength (&r, 20);
}

The Initialize function is used to modify the structure members and hence the function must be called by address. The ChangeLength function is also used to modify the length of the rectangle and hence this function going to be called by address. The Area function is basically used to calculate the Area and we don’t want to make any changes to the rectangle object and hence the Area function is going to be called by value.

Initialize Function:

The Initialize function taking one pointer variable. This pointer variable p will be a pointer to the rectangle object. Now using this pointer variable p, we can access the length and breadth of the rectangle object using the arrow -> operator. For better understanding, please have a look at the below code.

void Initialize (struct Rectangle *p, int l, int b)
{
    p->length = l;
    p->breadth = b;
}
Area Function:

The Area function takes one parameter of struct Rectangle type. As we are not using either & or *, so it is going to be called by value. As part of this Area method, a new rectangle object will be created with the values passed to it. Then it calculates the Area and returns it to the function from where it is being called. If you do any changes to the formal parameter rectangle object then it will not affect the actual parameter. For better understand, please have a look at the below code.

int Area (struct Rectangle r)
{
  return r.length * r.breadth;
}
ChangeLength Function:

The ChangeLength function is used to change the length of the Rectangle object. So, this function taking a parameter of structure Rectangle type, and hence it is called by address. Now, the pointer variable points to the Rectangle object, and using the arrow operator we can access the rectangle object and modify the length member as shown below.

void ChangeLength (struct Rectangle *p, int l)
{
    p->length = l;
}
The complete code is given below:
#include <stdio.h>
struct Rectangle
{
    int length;
    int breadth;
};

void Initialize (struct Rectangle *p, int l, int b)
{
    p->length = l;
    p->breadth = b;
}

int Area (struct Rectangle r)
{
    return r.length * r.breadth;
}

void ChangeLength (struct Rectangle *p, int l)
{
    p->length = l;
}

int main ()
{
    struct Rectangle r;
    Initialize (&r, 10, 5);
    Area (r);
    ChangeLength (&r, 20);
}
Conclusion:

This is the style of programming that leads to object orientation and this is the highest level of programming that we can do in C language by writing structures and functions. How it leads to object orientation and how it is highest lets us see.

The structure and the functions (Initialize, Area, and ChangeLength) are related to the structure Rectangle. So, in C programming this is the highest level of programming where we define a structure and we write all the function related to that structure because the grouping of data at one place is a structure, grouping the instructions for performing a task is a function, this is the style we follow in C language.

In the next article, I am going to discuss the Converting a C program to C++. Here, in this article, I try to explain Structure and Functions and I hope you enjoy this Structure and Functions article.

Leave a Reply

Your email address will not be published. Required fields are marked *