LOGO
11
MODULAR PROGRAMMINGMODULAR PROGRAMMING
Submitted to: Submitted by:Submitted to: Submitted by:
Mrs. Priyanka Soni Krati KatyalMrs. Priyanka Soni Krati Katyal
MCA 1MCA 1
stst
Sem Sem
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
Modular ProgrammingModular Programming
Modular programmingModular programming is a software design is a software design
technique that emphasizes separating the technique that emphasizes separating the
functionality of a functionality of a programprogram into independent, into independent,
interchangeable modules, such that each contains interchangeable modules, such that each contains
everything necessary to execute only one aspect of everything necessary to execute only one aspect of
the desired functionality.the desired functionality.
Need for Modular ProgrammingNeed for Modular Programming
•When a program becomes very large and When a program becomes very large and
complex, it becomes very difficult task for the complex, it becomes very difficult task for the
programmers to design, test and debug such a programmers to design, test and debug such a
program.program.
• Therefore a long program can be divided into a Therefore a long program can be divided into a
smaller program called modules as the modules smaller 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 and the task of programmer becomes easy and
convenient. convenient.
•It makes your program easy to understand.It makes your program easy to understand.
•Helps manage complexityHelps manage complexity
oSmaller blocks of codeSmaller blocks of code
oEasier to readEasier to read
•Encourages re-use of codeEncourages re-use of code
Within a particular program or across Within a particular program or across
different programsdifferent programs
•Allows independent development of codeAllows independent development of code
Why Modular Programming??Why Modular Programming??
ModularityModularity
•How do you solve a big/complex How do you solve a big/complex
problem?problem?
•Divide it into small tasks and solve each Divide it into small tasks and solve each
task. Then combine these solutions.task. Then combine these solutions.
Divide and ConquerDivide and Conquer
•In C we use In C we use functionsfunctions also referred to as also referred to as
modulesmodules to perform specific tasks that we to perform specific tasks that we
determined in our solutiondetermined in our solution
•This strategy is essentially based on the This strategy is essentially based on the
divide-and-conquer approach to problem divide-and-conquer approach to problem
solving and has many advantages over solving 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 and We will assign a name to each module and
combine the named modules in a program combine the named modules in a program
structure under the control of a main structure under the control of a main
program. program.
•Such a program structure consists of a Such a program structure consists of a
set of modules and an order of set of modules and an order of
execution.execution.
Elements of Modular ProgrammingElements of Modular Programming
•C requires that function names be unique in a C requires that function names be unique in a
source program file.source program file.
•Program execution always begins and eventually Program execution always begins and eventually
terminates in the main function.terminates in the main function.
•Additional functions are called or invoked when Additional functions are called or invoked when
the program encounters function namesthe program encounters function names
•Functions could be Functions 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)
•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;
}}
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;
}}
These functions require three elements:These functions require three elements:
1.1.Function declaration: In this only the Function declaration: In this only the
name and the syntax of the function will name and the syntax of the function will
written.written.
2.2. Function calls: In this program will call Function calls: In this program will call
the functionthe function
3. Function definition: In this program 3. Function definition: In this program
will defines that how the function will will defines that how the function will
perform there task.perform there task.
A function definition consists of A 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 in 3. 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…..
} }
#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 : %d\n", ret );printf( "Max value is : %d\n", ret );
Modular Programming ExampleModular Programming Example
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;
}}
OutputOutput
Max value is : 200Max value is : 200
Advantages of using modulesAdvantages of using modules
•Modules can be written and tested Modules can be written and tested
separatelyseparately
•Modules can be reusedModules can be reused
•Large projects can be developed in Large projects can be developed in
parallelparallel
•Reduces length of program, making it Reduces length of program, making it
more readablemore readable
•Promotes the concept of Promotes the concept of abstractionabstraction
A module hides details of a taskA module hides details of a task
We just need to know what this module We 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
Disadvantages of using modulesDisadvantages of using modules
•means documentation of modules must be means documentation of modules must be
systematicsystematic
•can lead to problems when modules are linked can lead to problems when modules are linked
because link must thoroughly testedbecause link must thoroughly tested
•Since separate modules map repeat certain Since separate modules map repeat certain
functions, the modular programming often functions, the modular programming often
need extra time and memory. need extra time and memory.