Modular Programming

 Module is the block of command. Modular programming is a technique of dividing program into small logical, manageable and functional module. Every modular program has one main module and many have many sub module. module is also known as procedure.

Advantages of modular programming:

  • many programmers can write different program module independently
  • debugging of the program become easier and faster
  • the procedure or module can be use in different place which reduces the program code
  • It improve the readability of program
  • It is easy to design code





In qbasic modular programming is handled by 2 ways:
a) Sub-procedure
b) Function-procedure

sub procedure
A procedure is a small manageable and functional part of a program that performs specific tasks and does not return any value so we used CALL keyword in SUB...END SUB. A sub procedure is needed to define before it is used in a program. A CALL statement is used to call the sub procedure in a program.

sub program is written with SUB ....... END SUB
important steps to create su procedure are as follows
1)Declaring sub procedure
            syntax: DECLARE sub name (parameter)
2) Defining sub procedure
            syntax: SUB name
3)calling sub procedure 
            syntax: CALL name

DECLARE SUB print_module( )
CLS
CALL print_module
PRINT "THIS IS LAST LINE WHICH PRINTS"
END

SUB print_module
    PRINT "yo print garni module yo"
    PRINT "learning sub procedure interesting"
END SUB

Formal and Actual parameter
Actual parameter is argument/parameter which is used to pass real value or data to procedure. It is also known as real parameter

Formal parameter are variable which are used to specify or declare types types of data to be passed to procedures either in sub or function. It is variable in sub module or procedure declaration which accepts data or variable passed to it from the calling module.


Call by Reference and Call by Value 
Difference between call by reference and call by value

Call be reference

Call by value

It is default mode of passing argument in qbasic

Argument are enclosed inside individual parenthesis

It required less memory

It requires more memory

Memory is allocated for both actual argument and formal argument

Memory is allocated only for actual argument and formal argument share memory

Can change the value of actual argument

Can’t change value of actual argument

It is very efficient

It is less efficient


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. Example: 
DECLARE SUB test (a, b) 
CLS
p = 10
q = 5
CALL test (p, q) 
PRINT p, q
END

SUB test (a, b)
a = a + 5 
b = b + 5
END SUB 
Output: 15 10 
In the above program, while calling the sub-program test, the parameter p and q are passed to the sub procedure test using call by reference method. In the sub module, reference/address of p and q are passed to sub module and stored in the local variable a and b respectively. When the value of 'a' is increased by 5, the change occurs in the variable 'p' of main module. In the same way, the value of ‘q’ of main module is also increased by 5. So, the values of 'p and q' are increased by 5 and the output is 15 and 10.


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 ( ). Example: 
DECLARE SUB display (p)
CLS n = 5
CALL display ((n))
PRINT n; 
END 
SUB display (p)
 p = p + 5
 END SUB 
In the above program, while calling the sub-program display, the parameter 'n' is passed to the sub procedure display using call by value method. In the sub module, value of 'n' is passed to sub module and stored in the local variable 'p'. When the value of 'p' is increased by 5, the change occurs in the variable 'p' of sub module as 'p' does not contain the reference of 'n'. So, the value of 'n' is not increased by 5 and the output is: 5



Local and Global variable
A variable which is define in module and is not accessible to any other modules is known as local variable. It is not accessible to the module where it is defined. The value of local variable is destroyed when execution of module is over.

A variable which can be accessed from any module or procedure of a program is called global variable. Variable can be made global by declaring them with DIM SHARED or COMMON SHARED statement or SHARED statement. The value of global variable is not destroyed during the execution of program.
example:
DECLARE SUB TEXT(c,d)
COMMON SHARED m
x=5
y=10
CALL TEST(x,y)
PRINT n
END

SUB TEST(c,d)
SHARED n
m=5
n=7
s=c+d
s=s+k
PRINT s
END SUB

In the above example , 'm', 'n' are global variables and 'x','y','c','d' and 's' are local variables.

Function Procedure:
is small manageable and functional part of program that perform specific  task and it returns a single value to the main program or calling module
Declaring , defining and calling are the required steps to create a function program. 3 important steps to create function procedure are discussed below.
Declaring a function procedure
Declaring function procedure means to specifying the function procedure's name and argument that will be used to call the procedure. Declare statement is used to declare function procedure
syntax:
DECLARE FUNCTION function_name(parameter)
for example 
DECLARE FUNCTION si(p,t,r)

Defining function procedure
After declare you sold define to include it in your program. Defining a function procedure always begins with a FUNCTION statement and end with an END FUNCTION statement. FUNCTION   END FUNCTION is an non executable statement that define user define function. To return value we must assign function_name in between FUNCTION   END FUNCTION
Calling function procedure
process of executing code inside FUNCTION   END FUNCTION is called calling. Function actually return some value so we don't need to use CALL statement.
the syntax for assigning a function to variable is

variable = function_name(parameter)
print "this is required answer";variable

example program
REM To calculate SI
declare function si(p,t,r)
cls
input "enter principal amount";p
input "enter the time in years";t
input "enter the rate";r
s=si(p,t,r)
print "simple interest is=";s
end

function si(p,t,r)
i = (p*t*r)/100
si= i
end function

Comments