Modular Programming in C Language
In this tutorial topic we will learn what is modular programming and how to use in C programming Language?.
A software is very large program that we use in our day to day life. Managing this kind of software is quite difficult. To manage large program or software one should divide the program in to small pieces of code, function or files etc.
writing program in to small piece of code is very easy to understand , manage,debug and test.
This concept is called modular programming.
Let Understand what is Modular Programming in Detail given in this tutorial.
Modular programing divids large program into small piece of code.
Modular Programming definition: the process of subdividing a computer program into a separate managable sub-programs is called modular programming.
Modular programming allows you to separeate your program into independent piece of code or function. Module is functional unit in program that does a well-defined task and called by each other as when needed.
some software or programs might have thousand and thousand of lines and to manage such type of program is difficult because presence of
i. logical error
ii. sysntax error in the program.
To manage such type of issues in programming the modular proramming is invented.
Let remeber some important point regarding Modular Programming.
Modular Programming:
1. User or Programmer can divide C program in smaller module.
2.Once the module is created can be reused in another prorams.
3.A Module is a separate managable,independent software component.
4. Each module contains something important and necessary to execute one aspect of the desired functionality.
5.Modular programming breaks large programs into small problems to improve readability,maintainability of the code and makes the program easy to correct errors or any changes in the future.
Advantages of Modular Programming.
1.Modules can be re-used in several projects or pull code from anywhere using libraries. Reusability reduces the size of the program.
2.Software is easily split and easier to test.
3.Using Module to fixing and finding bugs becomes very easy , smooth and maintainable.
4. Module reduces the conflict in the software projects or program.
5. Faster re-compilation, as only the modules that have been modified are actually re-compiled;
6. Easier debugging, as modules dependencies are clearly specified and every module can be tested separately.
7. Self-documenting, as the interface specifies all that we need to know to use the module.
DisAdvantages of Modular Programming.
Following are the some Disadvantes of Modular Programming.
1. It is a very challenging task for programmer to combine all the modules.
2. Modular programming product needs extra time and budget .
3.Modular programs need more memory space and extra time for execution.
4.Extra documentation is required so that other program modules are not affected.
5. function containing thousands lines of code reduces the program's efficiency because testing and debugging are time-consuming.
6. Different types of module may not have the same style.
Modular Programming Structure in C programming.
Let understand the modular programming from the Given Diagram .
Modern software systems typically consist of several modules that must interact to each other correctly in order to work properly.
Different components of a software system are divided into separate functional units called Modularity.
From the Given fig. we can observer that the Main program is a group of several modules.
Each module does a well-defined task and the modules may call to one-another as needed. The main program .
The main program is divided into three module module-1,module-2 and module-3.
where as module-1 is again divided in module-4 and module-5.
Each modules performs their unique individul task and may communicate to each other.
Modules makes program easy to test,debug and maintain.
Every module has it own specific task and can be use repeatedly in the program when ever required. Repeate use of module reduce program size and maintainability.Repeated use of module reduce program size and maintainability.
Module is seperate independent function code or program file that can be reuse in the progam.
for e.g header files
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
Program to Calculate the area and circumference of a circle Using Module in C programming.
We want to create a program that calculates the area and circumference of a circle. We can break down the functionality into two modules: one for calculating the area of circle and another for calculating the circumference of a circle.
First, we create a header file called "rectangle.h" which contains the function prototypes:
c
create a header file called "circle.h" which contains the function prototypes:
// circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
double calculateArea(double radius);
double calculateCircumference(double radius);
#endif
Next, we create a source file called "circle.c" which contains the implementations of the functions:
// circle.c
#include "circle.h"
double calculateArea(double radius) {
return 3.14*radius*radius;
}
double calculateCircumference(double radius) {
return 2 * 3.14*r;
}
Finally, in our main program we include the "circle.h" header file and use the functions from the "circle.c" module:
// main.c
#include <stdio.h>
#include "circle.h"
int main()
{
double r = 5.0;
double area = calculateArea(r);
double cirF = calculateCircumference(r);
printf("Area of Circle= %.2f\n", area);
printf("Circumference of Circle= %.2f\n", cirF);
return 0;
}
Output:
Area of Circle=78.50
Circumference of Circle=31.40