Modular Approach in Programming
Last Updated :
07 Sep, 2018
Modular programming is the process of subdividing a computer program into separate sub-programs. A module is a separate software component. It can often be used in a variety of applications and functions with other components of the system.
- Some programs might have thousands or millions of lines and to manage such programs it becomes quite difficult as there might be too many of syntax errors or logical errors present in the program, so to manage such type of programs concept of modular programming approached.
- Each sub-module contains something necessary to execute only one aspect of the desired functionality.
- Modular programming emphasis on breaking of large programs into small problems to increase the maintainability, readability of the code and to make the program handy to make any changes in future or to correct the errors.
Points which should be taken care of prior to modular program development:
- Limitations of each and every module should be decided.
- In which way a program is to be partitioned into different modules.
- Communication among different modules of the code for proper execution of the entire program.
Advantages of Using Modular Programming Approach -
- Ease of Use :This approach allows simplicity, as rather than focusing on the entire thousands and millions of lines code in one go we can access it in the form of modules. This allows ease in debugging the code and prone to less error.
- Reusability :It allows the user to reuse the functionality with a different interface without typing the whole program again.
- Ease of Maintenance : It helps in less collision at the time of working on modules, helping a team to work with proper collaboration while working on a large application.
Example of Modular Programming in C
C is called a structured programming language because to solve a large problem, C programming language divides the problem into smaller modules called functions or procedures each of which handles a particular responsibility. The program which solves the entire problem is a collection of such functions.
Module is basically a set of interrelated files that share their implementation details but hide it from the outside world. How can we implement modular programming in c? Each function defined in C by default is globally accessible. This can be done by including the header file in which implementation of the function is defined.
Suppose, we want to declare a
Stack data type and at the same time want to hide the implementation, including its data structure, from users. We can do this by first defining a public file called
stack.h which contains generic data Stack data type and the functions which are supported by the stack data type.
In the
header file we must include only the definitions of constants, structures, variables and functions with the name of the module, that makes easy to identify source of definition in a larger program with many modules.
Keywords
extern and
static help in the implementation of modularity in C.
stack.h:
extern stack_var1;
extern int stack_do_something(void);
Now we can create a file named
stack.c that contains implementation of stack data type:
stack.c
#include
int stack_var1;
static int stack_var2;
int stack_do_something(void)
{
stack_var1 = 2;
stack_var2 = 5;
}
The main file which may includes module stack
#include
int main(int argc, char*argv[]){
while(1){
stack_do_something();
}
}
Similar Reads
Modular Arithmetic for Competitive Programming In mathematics, modular arithmetic refers to the arithmetic of integers that wraps around when a certain value is reached, called the modulus. This becomes particularly crucial when handling large numbers in competitive programming. This article "Modular Arithmetic for Competitive Programming" will
11 min read
Blog | Programming Guidelines Computer programming is a process of writing an executable computer program for accomplishing a specific computer task. Although writing the programs is an art, however, there should be some minimum guidelines that should be followed while writing programs. Name: Define the variable names as per pro
2 min read
Functions in Programming Functions in programming are modular units of code designed to perform specific tasks. They encapsulate a set of instructions, allowing for code reuse and organization. In this article, we will discuss about basics of function, its importance different types of functions, etc.Functions in Programmin
14 min read
Object Oriented Programming | HCI In computer programming, Object-Oriented Programming (OOP) stands out as a paradigm that has converted the manner software program is conceptualized and built. OOP is based on foremost and concepts and plays a crucial role in the introduction of efficient and adaptable software solutions. This artic
8 min read
What is Imperative Programming? The computer programming paradigm defines the style of programming, approach to solve problem and method of computer systems towards providing solutions use programming. There is a classification of programming paradigms into two broad paradigms i.e., imperative and declarative. This article is base
6 min read