You are on page 1of 20

PROGRAMMING AND ALGORITHM

Modular Programming (Modularity)


Ria Hari Gusmita, ST, M.Kom
Imam M. Shofi, MT

ProdiTeknik Informatika
Fakultas Sains dan Teknologi
UIN Syarif Hidayatullah
Jakarta
Outline
Modularity concept
Subprogram (module)
The advantages of Modularity
Type of subprogram
Procedure
Global and local name
Parameters (Interfaces)
Modularity Concept
Large (complex) programs should be split into several
smaller subprograms
Each subprograms perform specific tasks (computing)
The good subprogram is the one that independent from the
main program
Technique that split a program into several subprograms
called modular programming techniques (modularization)
Subprogram
Has the same structure with the main program ie header,
declarations and algorithm
Subprograms executed by calling its name from the main
program
When an subprograms called, then the control program
moved to that subprograms. Furthermore, all instructions in
the subprograms will be executed. After the instruction is
executed, the control program returned to the main program
to run next instruction
The Advantages of Modularization
Provide facilities in writing the same section (instructions) in
a program. In other words, modularization avoid writing
repeated section in the program
Provide facilities to write and find mistakes (debug) program
Type of Subprogram
Procedure
Function
Procedure
Subprograms that do the specific work (activities) and
generate a net effect
The net effect is known by comparing the initial and final
conditions on the implementation of a procedure
Defining Procedures
Write the name of the procedure (with parameters if any), declare a variable
(variables, constants, types, etc.), and outlined a series of actions undertaken
Every procedure has a unique name and should be start with the verb. For
example, Compute_triangle_area, Find_Maximum_Value, etc
Parameters are used for the exchange of data / information between the
procedure and the main program (algorithm) in the calling location
There are two types of parameters ie actual and formal. Actual parameters
(arguments) is used at the time of the calling of the procedure. While the formal
parameters declared in the header of the procedure
Defining Procedures
Procedure Template :
Procedure ProcedureName(declaration of formal parameters, if
any)
{Procedure specification include of initial and final condition of
procedure}
DECLARATION :
{variables name}
ALGORITHM:
{sequence of instructions}
Examples of Procedures
procedure Decide_Maximum_Value (input a,b : integer)
{Decide the maximum value between two integer numbers,
a and b}
DECLARATION :
max : integer
ALGORITHM:
if a > b then
max a
else
max b
write(max)
end procedure
Examples of Procedures
procedure Display_Even_Number_Series
{Display even number series that less than 10}
DECLARATION :
number : integer
ALGORITHM :
number 2
while (number < 10) do
write(number)
number number + 2
endwhile
end procedure
Global and Local Name
Global name is declared in the algorithm / main program to be used in all parts of the
algorithm / program, including procedures in it
Local name is declared in the procedure that will only be known and used in the
procedure
Parameters (Interfaces)
Used to exchange data / information between the algorithm / main programs and
procedures
When the procedure is called, the actual parameters corresponding to formal
parameters
Each actual parameter is paired with the corresponding formal parameters
The number of actual parameters must match with the number of formal parameters
Each actual parameter must be the same type with the corresponding formal
parameters
Each actual parameter must be expressed in a manner consistent with the type of
formal parameters
Parameters (Interfaces)
There are three types of formal parameters :
1. Input parameters (input) / value parameter parameters whose value serves as
input for the procedure
2. Output parameters (output) which holds the output parameters (output) produced
by the procedure
3. Parameter input / output parameters that serve as input and output for the
procedure
Example of Procedures with Input Paramaters
procedure Calculate_average_value(input n : integer)
{Calculate the average value from n numbers}
DECLARATION :
i,number, sum : integer
average : real
ALGORITHM :
sum0
for i1 to n do
write(Enter number ,i)
read(number)
sumsum+number
endfor
averagesum/n
write(The average value is ,average)
end procedure
Example of Calling the Procedures with Input Paramaters
Program Calculate_average
{Calculate the average value from n numbers using
procedure}
DECLARATION :
n : integer
procedure Calculate_average_value(input n :
integer)
ALGORITHM :
write(how many numbers will be calculated ?)
read(n)
Calculate_average_value(n)
Example of Procedures with Output Paramaters
procedure Calculate_average_value1(output avg : real)
{Calculate the average value from n numbers}
DEKLARASI :
i,number,sum,n : integer
ALGORITMA:
write(How many numbers will be calculated ?)
read(n)
sum0
for i1 to n do
write(Enter number ,i)
read(number)
sum sum + number
endfor
avg sum / n
End procedure
Example of Calling the Procedures with Output Paramaters
Program Calculate_average2
{Calculate the average value from n numbers using
procedure}
DECLARATION :
procedure Calculate_average_value1(output avg :
real)
avg_value : real
ALGORITMA:
Calculate_average_value1(avg_value)
write(The average value is , avg_value)
Example of Procedures with Input/Output Paramaters
Procedure Calculate_the_square
(input/output number:integer)
{Calculate the square of a number}
DECLARATION:
{nothing}
ALGORITHM :
number number * number
End procedure
Example of Calling the Procedures with Input/Output Paramaters
Program Calculate_the_square_value
{Calculate the square value of a number using
procedure}
DECLARATION :
Procedure Calculate_the_square
(input/output number:integer)
num : integer
ALGORITHM :
write(Enter a number : )
read(num)
Calculate_the_square(num)
write(The square value is , num)

You might also like