Introduction:
Modular programming in Qbasic is a technique used to divide our program into many small logical, manageable and functional modules or blocks. However, every modular program has one main module and may have many sub modules. The module of a program is also called procedure Some advantages of modular programming in Qbasic are:

Advantages of Modular Programming
i. Firstly, many programmers can write different program modules independently.
ii. The debugging of the program becomes easier and faster.
iii. The same procedure can be used in different places, which reduces the program codes .
iv. Lastly, it improves the readability of a program. It is easy to design code.
Modular Programming in QBASIC
We can handle modular programming in QBASIC by two ways:
a) Sub-procedure b) Function-procedure
Creating Sub-procedure
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. As, a sub procedure needs to define before it is used in a program. A CALL statement is used to call the sub procedure in a program.
Syntaxt:
SUB
…..
END SUB statement.
There are two parts of sub-program in QBASIC:
a) Declaration Part
b) Definition Part
Declaration Part of Sub-Procedure
To create a sub-program in QBASIC, we need to declare at first. We use DECLARE statement for this purpose.
DECLARE statement
Purpose: a non-executable statement that declares references to QBASIC sub-program
Syntax:
DECLARE SUB <name>[(<Parameter List>)]
i. Firstly, name is the name of the sub-program (sub-procedure or procedure), which we use to call.
ii. At last, parameter list indicates the number and type of arguments that will be used to call the procedure
Example:
DECLARE SUB Sum (A, B)
In the above example, Sum is the name of the procedure whereas A and B indicates the parameter list.
Definition part of Sub-Program
The second step of creating a sub-program is to include the statement associated with its tasks. So, we use SUB….END SUB statement to define a sub-program.
SUB…END SUB statement
Purpose: a procedure statement that marks the beginning and end of a subprogram
Syntax: SUB [(parameter List)] ……….. END SUB
Example:
SUB Sum (P, Q)
c = P + Q
PRINT “Sum = “; c
END SUB
i. While defining a sub-procedure, press Enter key after typing Sub (Parameter List).
ii. After that, A new window will appear which is called a sub-module or a sub-procedure. The body of sub-program is written between
SUB… END SUB of this window.
iii. To switch between the windows: You can move either from sub-module to main-module or from main-module to submodule by pressing SHIFT+F2 keys at the same time. or You can press only F2 key to display the list of procedures along with main-module. You can choose the required name of procedure from this list to switch into it.
Executing sub-programs
To run a sub-program, you need to use CALL statement in the main-module.
CALL statement Purpose: a statement that transfers control to another sub procedure from main module.
Syntax: CALL <name>[(<Parameter List>)]
i. name is the name of the SUB
ii. At last, argument list lists the variables or constants passed to the SUB
Example: CALL Sum (P, Q)
Let’s see the whole program now:

Call by Reference and Call by Value
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.
Example:
DECLARE SUB test (p, q)
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 10.
Call by value
Call by value is a method of calling a procedure in which actual data are passed to the calling procedure module. Though, It is done by enclosing each parameter by a separate parenthesis ( ).
Example:
DECLARE SUB display (n)
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
Formal and Actual Parameter
Formal parameters are variables which are used to specify or declare types of data to be passed to the procedures either sub or function. Although, a formal parameter is always variable(s).
Actual or Real parameters are arguments which are used to pass real value or data to the procedure. However, actual parameters may be variables or constant values. An actual parameter can also be in the form of expression.


A variable in main module which can be accessed from any module or procedure of a program is known as a global variable. So that, variable can be made global declaring them with DIM SHARED or COMMON SHARED or SHARED attribute.
Declaring global variable in Main module:
COMMON SHARED and DIM SHARED statements are used to declare global variables in main module.
a) COMMON SHARED Statement
Syntax: AS, COMMON SHARED variable list variable list is a group a one or more variables separated by a comma.
Example:
COMMON SHARED a, b, c
b) DIM SHARED Statement
Syntax:
DIM SHARED variable list variable list is a group a one or more variables separated by a comma.
Example: DIM SHARED a, b, c
Declaring global variable in Sub-module:
SHARED statement is used to declare global variables in sub-module.
a) SHARED statement
Syntax:
SHARED variable list
variable list is a group of one or more variables separated by a comma.
Example: SHARED a, b, c
Example #1
DECLARE SUB test ()
COMMON SHARED a
CLS a = 10
CALL test
END
SUB test
PRINT a
END SUB
Output: 10
In this program, the variable ‘a’ is declared as global in main module using COMMON SHARED statement so, it is recognized in sub-module also. Therefore, the statement PRINT a gives 10 on the output screen.
Example #2
DECLARE SUB test ()
CLS CALL test
PRINT M
END ===========
SUB test
SHARED M
M = 5
END SUB
Output: 5
In this program, the variable ‘M’ is declared as global in sub module using SHARED statement so it is recognized in main-module also. Therefore, the statement PRINT M gives 5 on the output screen.
REm Common Shared list_of_var Dim Shared list_of_var
REm Global variable 2. Dim Shared
REm WAP to find volume of cylinder HInt V= PI*R^2*h
Declare Sub Cylinder_Vol(r,h)
cls
DIM SHARED Pi
Pi = 22 / 7
Input "Enter the radius of circle"; r
Input "Enter the height of cylinder"; h
Call Cylinder_Vol(r,h)
End
Sub Cylinder_Vol(r,h)
V = Pi * R ^ 2 * H
Print "The volume of cylinder is"; V
End Sub
Modular Programming in Qbasic examples :
Example #1
REM to calculate area of a circle
DECLARE SUB area (r)
CLS
INPUT “Type radius “; r
CALL area(r)
END SUB area (r)
a = 22 / 7 * r ^ 2
PRINT “Area = “; a
END SUB
Example #2
REM To display multiple table
DECLARE SUB MT (n)
CLS
INPUT “Type a number “; n
CALL MT(n)
END SUB MT (a)
FOR i = 1 TO 10
PRINT a; “x”; i; “=”; a * i
NEXT i
END SUB
Example #3 REM To check odd or even
DECLARE SUB check (n)
CLS
INPUT “Type any number “; p
CALL check(p)
END
SUB check (w)
IF w MOD 2 = 0 THEN
PRINT “Even”
ELSE PRINT “Odd”
END IF
END SUB
Example #4
REM To display first 10 natural numbers
DECLARE SUB series ()
CLS
CALL series
END
SUB series
FOR i = 1 TO 10
PRINT i; NEXT i
END SUB
Example #5
REM To check palindrome word or not
DECLARE SUB check (k$)
CLS
INPUT “Type a string “; s$
CALL check(s$)
END
SUB check (n$)
FOR i = 1 TO LEN(n$)
b$ = MID$(n$, i, 1) + b$
NEXT i
IF n$ = b$ THEN
PRINT “It is palindrome”
ELSE
PRINT “It is not palindrome”
END IF
END SUB
b) Creating Function Procedure
Function-procedure There are many functions including LEN( ), MID$( ), ASC( ) etc. available in QBASIC same as library function. Although they are not sufficient for programmers. So, in some cases, we have to make such function ourselves. This type of function is called user-defined function because it is created by the user.
A user-defined function is written with FUNCTION … END FUNCTION statement i.e; Declaration part
DECLARE statement Purpose: a non-executable statement that declares references to QBASIC
user-defined function Syntax:
DECLARE FUNCTION [(Paramenter List)]
i. name is the name of the function that will be used to call the function
ii. parameter list indicates the number and type of arguments that will be used to call the function.
Note: DECLARE statement cannot be used after any executable statements. The same DECALRE statement for function procedure is used to declare sub-program Example: DECLARE FUNCTION Area (L, B)
Definition part
FUNCTION…END FUNCTION statement
Purpose: a non-executable statement that defines the user-defined function
Syntax:
FUNCTION [(Paramenter List)]
{ Body of function }
Expression
END FUNCTION
i. name, name of the function
ii. parameter list is a group of one or more variable separated by commas that will be passed to the function when it is called
iii. At last, expression is the return value of the function
Example:
FUNCTION Area (P, Q)
A = P * Q
Area = A
END FUNCTION
Calling a User-Defined function
Since the function always returns a value,as we can assign this value to another variable or print directly on the output screen. CALL statement cannot be used in user-defined function. You can learn Library Function.
Example:
c = Area(L, B)
PRINT “Area is “; c
or
PRINT “Area is “; Area(L, B)

Modular programming in qbasic examples
Example #1
REM To calculate Simple Interest
DECLARE FUNCTION SI (a, b, c)
CLS INPUT “Type Principal “; p
INPUT “Type Time “; t
INPUT “Type Rate “; r
PRINT “Simple Interest = “; SI(p, t, r)
END
FUNCTION SI (p, t, r)
i = (p * t * r) / 100
SI = i
END FUNCTION
Example #2
REM To calculate circumference
DECLARE FUNCTION cir (r)
CLS
INPUT “Type radius of a circle “; r
c = cir(r)
PRINT “Circumference = “; c
END
FUNCTION cir (r)
circum = 2 * 22 / 7 * r
cir = circum
END FUNCTION
Example #3
REM To display the smaller number
DECLARE FUNCTION small (a, b)
CLS
INPUT “Type any two numbers “; x, y
PRINT “Smaller number is “; small(x, y)
END
FUNCTION small (a, b)
IF a < b THEN
small = a
ELSE
small = b
END IF
END FUNCTION