Saturday 8 June 2013

What is modular programming ?

Modular programming :

Modular programming is the concept of dividing your whole big programs in to many small programs. Using Modular Programming your program can be divided in to different Functions, files, modules etc..

lets see one example for a function:

Program without modular division :

#include <stdio.h>
#include <conio.h>
void main()
{
      int a, b, c, d, avg ; 
/*Taking input from user. Four numbers*/
      printf("Enter 4 numbers :");
      scanf("%d%d%d%d", &a, &b, &c, &d);
/*Here we are doing calculation for our program*/
      avg1 = (a+b)/2;
      avg2 = (c+d)/2;
      avg3 = (b+c)/2;
/*Printing output*/
    printf("avg1 = %d", avg1);
    printf("avg2 = %d", avg2);
    printf("avg3 = %d", avg3);

}

Now this program we can write like this also :
In Header.c file
#include <stdio.h>

In Func.c file
#include "FILE1.c"
int avg(int x, int y)
{
      return((x + y )/2);
}


In main.c file
#include  "FILE1.c"
#include  "FILE2.c"
 int a, b, c, d, avg1, avg2, avg3 ; 
/*Taking input from user. Four numbers*/
      printf("Enter 4 numbers :");
      scanf("%d%d%d%d", &a, &b, &c, &d);
/*Here we are doing calculation for our program*/
      avg1 = avg(a,b);
      avg2 = avg(c,d);
      avg3 = avg(b,c);
/*Printing output*/
    printf("avg1 = %d", avg1);
    printf("avg2 = %d", avg2);
    printf("avg3 = %d", avg3);

}

NOTE: If you are working with windows then no need to go for different file. Make it in one file.
For compilation in gcc :
gcc -o exe main.c func.c -I .
Get more details on compilation of Gcc Click here.

Here we have divided our one long program in to modules like functions, subroutines, files.
The benefit of this type of program is that :
1. You can use same function multiple time.
2. Debugging will be easier.
3. When going for writing new program you can use these module also, no need to write once again.
4. Good memory utilities.

Some more benefits are there..If you know some more comment below.

If you have any doubt or need any type of C program or want to contribute...click on Submit program

No comments:

Post a Comment