2. Topics Covered
• Modular Programing
• Need of Modular Programming
• Why Modular Programming??
• Modularity
• Elements of Modular Programming
• Modular Programming Example
• Advantages of Modular Programming
• Disadvantages of Modular Programming
• Reference
3. Modular ProgrammingModular Programming
Modular programmingModular programming is a software designis a software design
technique that emphasizes separating thetechnique that emphasizes separating the
functionality of afunctionality of a programprogram into independent,into independent,
interchangeable modules, such that each containsinterchangeable modules, such that each contains
everything necessary to execute only one aspect ofeverything necessary to execute only one aspect of
the desired functionality.the desired functionality.
4. Need for Modular ProgrammingNeed for Modular Programming
• When a program becomes very large andWhen a program becomes very large and
complex, it becomes very difficult task for thecomplex, it becomes very difficult task for the
programmers to design, test and debug such aprogrammers to design, test and debug such a
program.program.
• Therefore a long program can be divided into aTherefore a long program can be divided into a
smaller program called modules as the modulessmaller program called modules as the modules
can be designed, tested and debugged separately,can be designed, tested and debugged separately,
the task of programmer becomes easy andthe task of programmer becomes easy and
convenient.convenient.
• It makes your program easy to understand.It makes your program easy to understand.
5. • Helps manage complexityHelps manage complexity
o Smaller blocks of codeSmaller blocks of code
o Easier to readEasier to read
• Encourages re-use of codeEncourages re-use of code
Within a particular program or acrossWithin a particular program or across
different programsdifferent programs
• Allows independent development of codeAllows independent development of code
Why Modular Programming??Why Modular Programming??
6. ModularityModularity
• How do you solve a big/complexHow do you solve a big/complex
problem?problem?
• Divide it into small tasks and solve eachDivide it into small tasks and solve each
task. Then combine these solutions.task. Then combine these solutions.
Divide and ConquerDivide and Conquer
7. • In C we useIn C we use functionsfunctions also referred to asalso referred to as
modulesmodules to perform specific tasks that weto perform specific tasks that we
determined in our solutiondetermined in our solution
• This strategy is essentially based on theThis strategy is essentially based on the
divide-and-conquer approach to problemdivide-and-conquer approach to problem
solving and has many advantages oversolving and has many advantages over
developing a program for the entire problem.developing a program for the entire problem.
• We will assign a name to each module andWe will assign a name to each module and
combine the named modules in a programcombine the named modules in a program
structure under the control of a mainstructure under the control of a main
program.program.
8. • Such a program structure consists of aSuch a program structure consists of a
set of modules and an order ofset of modules and an order of
execution.execution.
9. Elements of Modular ProgrammingElements of Modular Programming
• C requires that function names be unique in aC requires that function names be unique in a
source program file.source program file.
• Program execution always begins and eventuallyProgram execution always begins and eventually
terminates in the main function.terminates in the main function.
• Additional functions are called or invoked whenAdditional functions are called or invoked when
the program encounters function namesthe program encounters function names
• Functions could beFunctions could be
Pre-defined library functions (e.g., printf, sin, tan)Pre-defined library functions (e.g., printf, sin, tan)
oror
Programmer-defined functions (e.g., my_printf,Programmer-defined functions (e.g., my_printf,
area)area)
10. • FunctionsFunctions Perform a specific taskPerform a specific task
Arguments with no return typeArguments with no return type
void fun(int x, int y)void fun(int x, int y)
{{
//statement//statement
}}
Arguments with return typeArguments with return type
int fun(int x, int y)int fun(int x, int y)
{{
//statement//statement
return 0;return 0;
}}
11. No arguments with no return typeNo arguments with no return type
void fun( )void fun( )
{{
//statement//statement
}}
No Arguments with return typeNo Arguments with return type
int fun( )int fun( )
{{
//statement//statement
return 0;return 0;
}}
12. These functions require three elements:These functions require three elements:
1.1.Function declaration: In this only theFunction declaration: In this only the
name and the syntax of the function willname and the syntax of the function will
written.written.
2.2. Function calls: In this program will callFunction calls: In this program will call
the functionthe function
3. Function definition: In this program3. Function definition: In this program
will defines that how the function willwill defines that how the function will
perform there task.perform there task.
13. A function definition consists ofA function definition consists of
1. A function type1. A function type
2. A function name2. A function name
3. An optional list of formal parameters enclosed in3. An optional list of formal parameters enclosed in
parenthesesparentheses
4. A compound statement.4. A compound statement.
Ex : Void start(int a)Ex : Void start(int a)
{{
…….statement…...statement…..
}}
14. #include <stdio.h>#include <stdio.h>
int max(int num1, int num2);int max(int num1, int num2);
int main ()int main ()
{{
int a = 100;int a = 100;
int b = 200;int b = 200;
int ret;int ret;
ret = max(a, b);ret = max(a, b);
printf( "Max value is : %dn", ret );printf( "Max value is : %dn", ret );
Modular Programming ExampleModular Programming Example
15. return 0;return 0;
}}
int max(int num1, int num2)int max(int num1, int num2)
{{
int result;int result;
if (num1 > num2)if (num1 > num2)
result = num1;result = num1;
elseelse
result = num2;result = num2;
return result;return result;
}}
17. Advantages of using modulesAdvantages of using modules
• Modules can be written and testedModules can be written and tested
separatelyseparately
• Modules can be reusedModules can be reused
• Large projects can be developed inLarge projects can be developed in
parallelparallel
• Reduces length of program, making itReduces length of program, making it
more readablemore readable
• Promotes the concept ofPromotes the concept of abstractionabstraction
A module hides details of a taskA module hides details of a task
We just need to know what this moduleWe just need to know what this module
doesdoes
We don’t need to know how it does itWe don’t need to know how it does it
18. Disadvantages of using modulesDisadvantages of using modules
• means documentation of modules must bemeans documentation of modules must be
systematicsystematic
• can lead to problems when modules are linkedcan lead to problems when modules are linked
because link must thoroughly testedbecause link must thoroughly tested
• Since separate modules map repeat certainSince separate modules map repeat certain
functions, the modular programming oftenfunctions, the modular programming often
need extra time and memory.need extra time and memory.