0% found this document useful (0 votes)
2 views13 pages

Modularity Functions Procedures - SV

Chapter 5 introduces modular programming, a design technique that divides programs into independent, reusable modules to simplify complex problems. It discusses the advantages of modular programming, including ease of understanding, debugging, maintenance, and parallel development. The chapter also covers functions and procedures, their definitions, interfaces, and parameter passing methods.

Uploaded by

falfalah208
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views13 pages

Modularity Functions Procedures - SV

Chapter 5 introduces modular programming, a design technique that divides programs into independent, reusable modules to simplify complex problems. It discusses the advantages of modular programming, including ease of understanding, debugging, maintenance, and parallel development. The chapter also covers functions and procedures, their definitions, interfaces, and parameter passing methods.

Uploaded by

falfalah208
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CHAPTER 5 : INTRODUCTION TO

MODULAR PROGRAMMING
(PROCEDURES AND FUNCTIONS)

ASD 2025-2026
MODULAR PROGRAMMING

 Modular Programming is a software design technique that aims dividing a


program into separate, independent, and reusable modules.
 Each module encapsulates a specific functionality
 Each module can be developed, tested, and maintained independently before
being integrated into the overall system.
MODULAR PROGRAMMING – KEY
CONCEPT

Modularity aims to achieve two fundamental objectives.


 Transform a problem, sometimes extremely complex, into "n" simple problems
that can be solved separately.
 Find the solution to a problem only once, because once a module that performs
a specific task is built, tested, and saved, it can be reused as many times as desired
ADVANTAGES OF MODULAR PROGRAMMING

 Easy to understand – Each module focuses on one task


 Reusability – Modules can be reused in other programs
 Easy debugging – Errors are isolated within modules
 Easy maintenance – Changes in one module do not affect others
 Parallel development – Different programmers can work on different
modules
 Improved readability – Code is well-structured and organized
FUNCTION AND PROCEDURE IN
ALGORITHMIC PROGRAMMING

 In algorithmic modular programming, a program is divided into


[Link] subprograms are mainly of two types:
 Function
 Procedure
FUNCTION AND PROCEDURE IN ALGORITHMIC
PROGRAMMING

 A function is a subprogram that: Performs a specific task and Returns a value


to the calling program
 A procedure is a subprogram that Performs a specific task and does NOT return a
value. Output is usually displayed or modified via parameters
 Generally if the module has only one output, and this output is elementary data,
FUNCTION is used , otherwise it is a PROCEDURE (i.e. 0 to n outputs of any type).
INTERFACE OF A FUNCTION AND PROCEDURE

 The interface of a function or procedure is the formal specification that


describes how the function or procedure is used, without revealing how it is
implemented.
 The interface consists of:
 Name of the function/procedure
 Number of parameters (variables)
 Order of parameters
 Data types of parameters
 parameters passing (value or variable )
 Return type (for functions only)
DEFINING AND CALLING A FUNCTION
 How to declare and define function ?
A function has a name, parameters, a body, and a return value.
Function Name (parameter ):type
Body
 Example:
function Square(x: Integer): Integer;
begin
Squarex * x / or return x*x
end;
 Calling a function means using it to get a result. We use the name of the function
and the real parameters
result square(5)
EXAMPLE 2 : POWERX
Power1: calculates x^y

function Power1 (x: Integer; y: Integer): Integer;


var
i, result: Integer;
begin
result  1;
for i from 1 to y do
result result * x;
Power1 resut or (return result)
End;
PARAMETER PASSING

 Parameter passing is how values are given to functions or procedures.

 Use pass by value when the module should not change the input parameter.
 Use pass by variable (pass by reference) when the parameter is modified
and the updated value is required as output; in this case, the formal parameter is
preceded by VAR

 In general; we use :
-Pass by variable for ALL the output parameters of a procedure-type module.
-Pass by value for a function's input parameters
PROCEDURE DECLARATION AND CALLING

 A procedure is defined by its name, parameters, and body of instructions.


 Outputs Parameters preceded by the keyword VAR are passed by reference
 Example
PROCEDURE Divide(A, B : INTEGER;VAR Q, R : INTEGER);
BEGIN
Q := A DIV B; { Quotient }
R := A MOD B; { Remainder }
END;
PROCEDURE DECLARATION AND CALLING

 we call the function using reel parameters


 example
 Varibles Quotient, Remainder : INTEGER;
 BEGIN
 Divide(17, 5, Quotient, Remainder);
 WRITELN('Quotient = ', Quotient);
 WRITELN('Remainder = ', Remainder);
 END.
 Quotient = 3 and Remainder = 2
EXERCISES

 Write an procedure that allows determining whether an integer is odd or even.


 Use the pervious procedure and write an algorithm to determine the parity (odd or even)
of integers between a lower bound L and an upper bound U.
 Write a procedure to solve a quadratic (second-degree) equation whose coefficients A, B,
and C are read from the keyboard.
 Use the pervious procedure and write a program that solves first-degree and second-
degree equations.
 Discussed and solved during the lecture session.

You might also like