3.1 Modular Programming
Modular programming
Modular programming is a technique used to divide our program
into many small logical, manageable and functional modules or blocks.
Advantages of modular
programming
l Many
programmers can write different program modules independently.
l The
debugging of the program becomes easier and faster.
l The
same procedure can be used in different places, which reduces the program
codes.
l It
improves the readability of a program.
l It
is easy to design code.
Types of Procedure in Modular Programming
a)
Sub-procedure
b)
Function-procedure
Main Module -
The main part of the program inside which several submodules are created.
Sub-Program - A
group of statements written in a separate module to perform one or more tasks
Sub module / Procedure
Module
is a block of statement that solves a particular problem.
SUB procedure
A sub-procedure is a small manageable and functional part of a
program that performs specific tasks and does not return any value to the
calling module.
A CALL
statement is used to call the sub procedure in a program.
SUB-procedure’s
name does not accept data type symbol because it does not need to return a
value.
Passing arguments by value (Call
by value)
Call by value
is a method of calling a procedure in which actual data are passed to the
calling procedure module. It is done by enclosing each parameter by a separate
parenthesis ( ).
Passing arguments by reference (Call
by Reference)
Call by
reference is a method of calling a procedure in which the reference (address)
of parameters are passed to the calling procedure module instead of actual
value. By default, the parameters are passed using call by reference method.
Formal parameters
Formal
parameters are variables which are used to specify or declare types of data to
be passed to the procedures either sub or function. A formal parameter is
always variable(s).
Actual or Real parameters
Actual or Real parameters are arguments which are used to pass
real value or data to the procedure.
Actual
parameters may be variables or constant values or can also be in the form of
expression.
Local variable
A variable
which is defined in a module and is not accessible to any other modules is
known as local variable
It is only
accessible to the module where it is defined.
Value of local
variable is destroyed when execution of module is over.
Global variable
A variable in
main module which can be accessed from any module or procedure of a program is
known as a global variable.
Variable can be
made global declaring them with DIM SHARED or COMMON SHARED or SHARED statement.
FUNCTION procedure
A
function-procedure is a small manageable and functional part of a program that
performs specific tasks and returns a value to the calling module.
FUNCTION-procedure
is called by statement and expression method.
FUNCTION-procedure’s
name accepts data type symbols such as $, %, !, #, &, etc. and it depends
on the type of value to be returned. E.g.: FUNCTION REV$ returns string.
String function [SEE 2074 U]
String function is
used with string it returns string value.
Numeric function [SEE 2074 U]
Numeric function
is used with number it returns numeric value.
Library functions [SQE 2074K]
Library
functions are built-in or readymade functions provided by QBASIC. E.g. MID$( ),
LEN( ), SQR( ) etc.
User defined function
Function
which is defined by the user according to the need is called user defined
function.
CALL statement [SLC 2071] [PMT
2075K]
The function of CALL statement is to transfer control to another sub
procedure from main module.
DECLARE statement
The
function of DECLARE statement is to declare procedure such as FUNCTION or SUB
in modular programming.
DIM SHARED / COMMON SHARED statement
- to declare global variables in main module.
SHARED - to
declare global variables in sub module
No comments:
Post a Comment