In: Computer Science
Explain the importance of program modularization using at least one example.
300 words
Modular programming is a software design technique that focus attention on highlight separating the functionality of a program into independent modules, such that each contains everything necessary to execute only one aspect of the desired functionality.
Concept of Modularization
One of the most important concepts of programming is the ability to group some lines of code into a unit that can be included in our program.
The original words for this is a sub-program.
Other names include:
We are use the term function for that is what they are called in most of the main programming languages of today.
Functions are important because they allow us to take large complicated programs and to divide them into smaller pieces. Because the function is a smaller piece of the program, we can concentrate on what we want it to do and test it to make sure it works properly. Generally, functions fall into two categories:
1. Program Control – Functions used to simply sub-divide and control the program. These functions are unique to the program being written
2. Specific Task – Functions designed to be used with several programs. These functions perform a specific task and thus are usable in many different programs because the other programs also need to do the specific task
Depending on the programming language, there is a formal way to:
Program Control Function
The main program piece in many programming languages is a special function with the identifier name of main. The special or uniqueness of main as a function is that this is where the program starts executing code and this is where it usually stops executing code.
Specific Task Function
We often have the need to perform a specific task that might be used in many programs.
Advantages:
==> Development Can be Divided
Modular Programming allows development to be divided by splitting
down a program into smaller programs in order to execute a variety
of tasks.
This enables developers to work simultaneously and minimizes the
time taken for development.
==> Readable Programs
Modular Programming helps develop programs that are much easier to
read since they can be enabled as user-defined functions.
A program that carries multiple functions is easier to follow,
whereas a program that does not have a function is much harder to
follow.
==> Collaboration
With Modular Programming, programmers can collaborate and work on
the same application. Designing a program also becomes simpler
because small teams can focus on individual parts of the same
code.
==> Improves Manageability
Having a program broken into smaller sub-programs allows for easier
management. The separate modules are easier to test, implement or
design. These individual modules can then be used to develop the
whole program.
==> Allows Re-Use of Codes
A program module is capable of being re-used in a program which
minimizes the development of redundant codes. It is also more
convenient to reuse a module than to write a program from start. It
also requires very little code to be written.
==> Programming Errors are Easy to
Detect
Modular Programming minimizes the risks of ending up with
programming errors and also makes it easier to spot errors, if
any.
This is because the errors can be narrowed down to a specific
function or a sub-program.
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 linked 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();
}
}