You are on page 1of 23

INTRODUCTION TO

ALGORITHMS & PROGRAMMING

Modular Programming

M.Sc. Ibrahim Mesecan


imesecan@epoka.edu.al

Ibrahim
Mesecan
Page 1
Outline
In this chapter, you’ll understand :
 what modular programming is.
 why to use modular programming
 the usage of Functions without arguments
 the usage of Functions with input arguments
 Pass by value
 the usage of Functions with input&output
arguments
 Pass by reference
 Local and global variables
Ibrahim
Mesecan
Page 2 Modular Programming
Modular Programming

The idea of using a procedure to implement a line


of pseudocode is very important and, as you
develop more programming skills, it will become an
integral part of how you write a program.
Using top-down design method, we divide the
main task into major subtasks, and then continue to
divide the subtasks (stepwise refinement) into
smaller subtasks until all subtasks can be easily
performed.

Ibrahim
Mesecan
Page 3 Modular Programming
Modular Programming
Once an algorithm for solving a problem has been
developed using top-down design, the programmer
then writes code to translate the general solution into
the desired programming language.
Because each module can be tested independently,
modular programs are easier to test, debug, and
correct than programs that are not modular. Then,
once a module is running correctly, it can become
part of a longer program. This independent testing of
modules is referred to as bottom-up testing.

Ibrahim
Mesecan
Page 4 Modular Programming
Modular Programming

There are two essential points in programming:


Operation abstraction: To hide the operational
details from others
Data abstraction: To hide the data details from
the people who does not need

Ibrahim
Mesecan
Page 5 Modular Programming
Why Modular Programming?
 It helps designing the large programs easily using
top down design methods.
 Programs can be designed abstractly and the details can
be dealt later.
 Software Engineering: Large groups of programmers can
work on the same project. Program can be divided into
smaller subprograms. Everybody knows all subprograms
and their usage. The details of any subprogram is the
work of another programmer.
 Operational abstraction
 Data abstraction

Ibrahim
Mesecan
Page 6 Modular Programming
Why Modular Programming?
 It helps writing codes that can be read and written
easily.
 Helps reducing the code sizes. Subprograms can be
written once but can be used many times.
It helps writing programs faster. A subprogram can
easily be ported into another subprogram.
 Modules can be tested and designed independently,
thus, helping debugging and writing more reliable
programs.

Ibrahim
Mesecan
Page 7 Modular Programming
Functions without arguments
 It is important to develop the habit of using descriptive
names for all identifiers. For example, if you are going to
write a procedure/function to print a heading for the output,
PrintHeader might be a good choice.
 This allows you to recognize the task the
procedure/function is supposed to accomplish and makes
the program more readable. Each of the following would be
a reasonable, descriptive function heading.
void PrintHeader();
int GetN();
void PrintFooter();
Ibrahim
Mesecan
Page 8 Modular Programming
Functions without arguments
void PrintHeader()
{
cout<<"//////////////////////////////////”;
cout<<"/ /”;
cout<<"/ Sifa Medical Center /”;
cout<<"/ -------------------------- /”;
cout<<"/ 2693 Central City IZMIR /”;
cout<<"/ Phone (232) 222-5555 /”;
cout<<“//////////////////////////////////”;
}

Ibrahim
Mesecan
Page 9 Modular Programming
Functions without arguments
 If you want your GetN() module to return a number between 1 and 100;
int GetN(){
General Form of a Function
int n=0;
while(n<1 || n>100){ Fn_Type Fn_Name(){
cin>>n; …
} your operations;
return n; …
} return result;
}
Then in the main program you can just say,
int main()
{
int N;
N=GetN();
return 0;
}
Ibrahim
Mesecan
Page 10 Modular Programming
Functions with arguments

 Because procedures (functions) with no arguments


have too restricted usage, they are not common. The
arguments for procedures have two types:
1. Input parameters (Pass by value)
2. Output parameters (Pass by reference)

Ibrahim
Mesecan
Page 11 Modular Programming
Functions with input arguments
 Because according to every different input they can
produce different outputs, functions with arguments are
more common.
 You already use many standard functions, like:
sqrt(9)  calculates the square root of the given number (9).
9 is the input parameter here and according to the input parameter sqrt()
function calculates and returns the desired output(3).
 Everytime you call the sqrt() function with a different
parameter (number), it will calculate the sqrt of that given
parameter.

Ibrahim
Mesecan
Page 12 Modular Programming
Functions with input arguments
 Declaration
After the function name, in parentheses, for every
parameter you provide the parameter type followed by the
parameter name . You can have as many parameters as you
need.
General Form of a Function
F_Type F_Name (Pr1_type Pr1_name, Pr2_type Pr2_name,…)
{
variable declarations;

your operations;

return result;
Ibrahim }
Mesecan
Page 13 Modular Programming
Functions with input arguments

Example
If you want a function to calculate that calculates the
Euclidean distance of two points(x1,y1 and x2,y2);

float distance (int x1, int y1,


int x2, int y2)
{
float res;
int xd = x1 - x2;
int yd = y1 - y2;
res = sqrt(xd*xd + yd*yd);

return res;
Ibrahim }
Mesecan
Page 14 Modular Programming
Functions with input&output arguments
Declaration
The declaration of input&output parameters are
the same as input parameters, but, every output
parameter name is preceded by “&” operator.
Output parameters are also called “pass by
reference” parameters.
Normally, the functions return one output directly
to the name of the function. Output parameters
are needed when your function is needed to
return more than one output.
Ibrahim
Mesecan
Page 15 Modular Programming
Functions with input&output arguments

Declaration
In the following example parameter a is declared as
input parameter, but parameters b and c are input
and output parameters.
Output parameters
Fn_type Fn_name (int a, int &b, int &c) {
variable declarations;

your operations;

return result;
}
Ibrahim
Mesecan
Page 16 Modular Programming
Functions with input&output arguments

Constraints (Limitations for usage)


Output parameter can accept only the same
type corresponding variable whereas
Input parameter can have a corresponding
value or same type variable.

Ibrahim
Mesecan
Page 17 Modular Programming
Functions with input&output arguments

Constraints

When calling this function;


void f(int x, int& y, const int& z)
{ x += z; y += z;
cout << "x = " << x << ", y = " << y << ", z = " << z << endl;
}
After you run the code below ,
int main(){ what will the program show?
int a=3, b=4, c=5;
f(a,b,c); // OK
f(3,a,b); // OK
f(3,4,5); // not OK, because 2nd and 3rd parameters
must be same type variables
Ibrahim
Mesecan
Page 18 Modular Programming
Local and global variables
 The variables declared in the function signature and in the
function are called local functions. They are initialized and
alive while the program control is in the function. When
the program control leaves the function they are de-
allocated from the memory. Any call to these local
variables will cause error.
void f(int x, int& y)
{ x += 2;
y += 3;
cout << "x = " << x << ", y = " << y << endl; // OK
}
int main(){
int a=3, b=4;
cout << “a = " << a << ", b = " << b << endl; // OK
Ibrahim cout << "x = " << x << ", y = " << y << endl; // not OK
Mesecan
Page 19 Modular Programming
Local and global variables
 Local variables are not directly initialized, if you need to
use them in the right side of expressions first you must
initialize them. The parameters in the function signatures
get their initial values when they are called. But the
variables declared in the function body (including the
main() function) must be initialized.

void f(int x, int& y)


{ int z=2, k; // z is OK initialized, but, k is not
x += z; // is OK, it takes value as parameter
cin>>k; // k has been initialized here
y += k;
…..
}
Ibrahim
Mesecan
Page 20 Modular Programming
Modular Programming

Example:

long fact(int n){


// n! = n*(n-1)*(n-2)*...*(2)(1)
if (n < 0) return 0;
int res = 1;

for(int i=1; i<=n; i++) res *= i;

return res;
}

Ibrahim
Mesecan
Page 21 Modular Programming
Modular Programming

Example:

long Power(int n, int m){


int res = 1;

for(int i=1; i<=n; i++)


res *= m;

return res;
}

Ibrahim
Mesecan
Page 22 Modular Programming
Modular Programming

In class Examples:
1. Write a function that calculates sum of two
given numbers a and b
2. Write a program that calculates sum of the
odd numbers from A to B.

Ibrahim
Mesecan
Page 23 Modular Programming