MODULAR PROGRAMMING


The process of breaking down the large program into small manageable tasks is called modular programming.

ADVANTAGES OF MODULAR PROGRAMMING:

·        It reduces data redundancy

·        It is easy to understand

·        It is easy to test and debug

·        Its’ program management is easier

 

#MODULE:

A module is a source file containing sub or function procedure that can separately be compiled.

In modular programming, a program can be divided into two types:

1.     Main module

2.     Sub module

MAIN MODULE: It is the controlling section of modular programming. Whenever a program is opened, main module is seen and we can go to sub module by pressing ctrl + F2 or simply by pressing F2 key.

SUB MODULE: It is a program written under main module. A modular program may have more than one sub-module. We can use the same procedure in the same procedure whenever required by calling the procedure known as procedure call.

#PROCEDURE: A procedure is a collection of code to perform an action. There are two types of procedure:

1.     SUB procedure

2.     FUNCTION procedure

#PARAMETERS: Parameters are the pre-defined variable that indicate the input given to the sub procedure or function procedure.

#ARGUMENTS: Arguments are the actual values that are passed to the sub procedure. Arguments are also known as real to actual parameters.

#SUB procedure:

It is a set of instruction to perform a specific action. It doesn’t contain any suffix like (%, &, !, #, $)  sign after the name of procedure as it does not return any value. It is written as SUB…END SUB.

Features of SUB procedure:

·        Sub procedure does not return any value from sub module to main module.

·        It is called CALL statements.

·        It does not consider data type.

·        It can pass the parameter by both reference and value.

Creating sub procedures includes:

1.     Declaring a sub procedure: It means specifying the name of procedure, name of parameter along with its data type. DECLARE statement is used to declare a sub procedure.

SYNTAX;

DECLARE SUB <name of procedure> (parameter list)

 

2.     Defining a sub procedure: In QBASIC, sub procedure is defined by SUB…END SUB. SUB indicates the beginning of the sub procedure and END SUB indicates the end of the sub procedure.

SYNTAX;

DECLARE SUB <name of procedure> (parameter list)

        <statements>

END SUB

 

3.     Calling a sub procedure: Sub procedure can be called from any part of the program by using CALL statement. CALL statement sends the control of program to the called procedure and after execution of sub procedure, the main module or the place from where it is called and thus program ends from the main module with the END statement in the main module.

SYNTAX;

CALL <name of procedure> (parameter list)

 

#FUNCTION procedure:

A function is a program that performs the specific tasks and returns a single value to the main module. A function is written with FUNCTION…END FUNCTION statement.

Features of function procedure:

 

·        Function returns a single value to the main module.

·        It can be called by expression or PRINT statement.

·        It considers data type of the returned value.

·        It can also pass the parameters by both reference and value.

  

           Creating function procedure includes;

1.     Declaring a function procedure: It means specifying the name of procedure, name of parameter and its data type. DECLARE statement is used to declare function procedure.

SYNTAX;

DECLARE FUNCTION <name of function procedure> (parameter list)

 

2.     Defining a Function procedure: In QBASIC, Function procedure is defined by FUNCTION…END FUNCTION. While defining function procedure, all the statements required for program are kept in between FUNCTION and END FUNCTION statement.

SYNTAX;

FUNCTION <name of function> (parameter list)

       <statement>

END FUNCTION

 

3.     Calling a function procedure: Function procedure can be called by writing function name followed by arguments within the parenthesis like sum (a, b). It sends the control of program to the procedure and after execution of function procedure, the value is returned to the main module by storing the result in the function name and print the result in the main module.

SYNTAX;

<name of function> (parameter list)

#There are two ways of invoking the modules or passing the Arguments, they are;

1.     Arguments passed by reference: It means that the address of each argument is passed to the procedure by placing the address on the stack.

2.     Arguments passed by value: It means while passing the arguments, the value is only passed to the procedure and the reference or address remain in the main module.

#LOCAL AND GLOBAL VARIABLES

#Local variable: By default, variables declared inside the procedure is local. The variables cannot perform any task outside the procedure. Local variable is used only within a sub module.

Example;

SUB SUM (X, Y, Z)

Z=X+Y [Here, Z is the local variable]

PRINT Z

END SUB

 

#Global variable: The variable which is declared outside the function is called global variable which is also accessible for outside world. Global variable can be used both inside and outside the module. Global variables are created using the keywords like SHARED, DIM SHARED (in case of array) and COMMON SHARED before the variable name.

1.SHARED: It makes a variable global, and is declared in the sub procedure in order to share the variables of main modules without passing.

2.COMMON SHARED: It is a keyword which actually makes the variable global and is declared before the DECLARE statement in the main module in order to share it with any procedure.

3.DIM SHARED: It makes an array variable global and can be shared to procedure without actually passing.

 

#Disadvantages of modular programming:

·        Can lead to problems with variables names.

·        Can lead to problems when modules are linked because link must be thoroughly tested.

#Difference between sub procedure and function procedure.

Sub Procedure

Function Procedure

1.It does not return any value.

1.It returns a single value.

2.It uses CALL statement to invoke.

2.It is invoked by FUNCTION name.

3.It does not require return type data.

3.It requires a return type data.

 

#Differences between local and global variable.

Local Variable

Global Variable

1.It is declared and destroyed in the procedure.

1.This variable can be accessed from any procedure in the program without passing.

2.Scope of local variable is inside the sub procedure only.

2.Scope of global variable is everywhere as it can be accessed from any procedure.

 

#DEF FN: DEF FN is a procedure statement that defines and names a function. It does not support function procedures rather it uses relations of mathematics and gives the output in the same module.

SYNTAX;

DEF FN name (parameter list) = expression

Example;

DEF FNseries (I, N) = I^2*N

CLS

N=2

FOR I = 1 TO 10

PRINT FNseries (I, N );

NEXT I

END

#Static variable: A local variable whose value is preserved with the consecutive calls and declared with the keyword STATIC is static variable.

 

 

Sujit Prasad Kushwaha

A Dedicated Blogger Sharing Insights and Making a Difference.

Post a Comment

The comment section is your chance to make your mark. Don't hold back - add to the discussion and let your personality shine through!

Previous Post Next Post