2. Overview
• Modular programming is a technique of
developing software by separating the
functionality of a program into independent,
interchangeable modules that are combine
together to get the final working software.
3. Cont….
• Every modular program has one main module
and may have many sub modules.
• Main module is the top level module in which
program entry and exist takes place.
• Sub module is a block of statement that is
used for specific task and is controlled by main
module.
4. Structure of Modular Programming
Main
Program-M1
Module-M2
Module-M5 Module-M6
Module-M3 Module-M4
Module-M7
5. Cont…
• Qbasic programs consist of one or more
modules.
• A module is a block of statement that solve
particular problem.
• A module may contain SUB and Function
procedure, as well as code not part of SUB or
Function.
6. Advantages of modular programming
• The modular design offers several advantages
such as:
a. Improving the readability of a program.
b. Reducing redundancy.
c. Saving the time during project development.
d. Modules can be tested independently.
e. Program maintenance becomes easier.
7. Types of Procedures in Qbasic program
• In Qbasic, a programmer can name a block of
code which can be executed by simply calling
out that name. These name blocks of code are
called procedures.
• The two types of procedures are:
i. SUB Procedure
ii. Function Procedure
8. SUB Procedure
• SUB procedure is a block of statements which
is identified by a unique name that performs a
specific task.
• The block of statements is placed with a pair
of SUB /END SUB statements and can be
called by its name.
9. Features of SUB Procedure
• SUB procedure cannot be a part of an
expression and does not return any value.
• SUB procedure can be recursive. They may call
themselves.
• SUB procedure do not have data type.
• The parameters can be passed by reference or
by value.
10. Declaring SUB Procedure
• The DECLARE statement is used to declare a
SUB procedure.
• The syntax for the DECLARE statement is as
follows:
– DECLARE SUB name (parameter list)
– Example:
DECLARE SUB area(L,B)
Sub procedure area with two parameters L and B
DECLARE SUB Display()
Sub procedure Display with no parameters
11. Defining a SUB Procedure
• The SUB….END SUB statement is a procedure
statement that marks the beginning and
ending of a subprogram. The syntax is:
SUB Procedure_name[parameter,(list..)]
…’procedure variable definitions and
statements
END SUB
12. Calling a SUB Procedure
• The CALL statement is used to transfer control
to another procedure, a BASIC SUB program.
The syntax is :
CALL name (argument lit)
Example:
CALL area(r)
13. Example
• Write a program to create a procedure using
SUB…END SUB to input the value of length,
breadth and height of a box. The program
should calculate the area and volume of a box.
• Answer:
• DECLARE SUB area (l!,b!,h!)
• DECLARE SUB vol (l!,b!,h!)
• INPUT”Enter a length of a box”; l
14. Cont….
INPUT”Enter a breadth of a box”;b
INPUT”Enter a height of a box”;h
CALL area(l,b)
CALL vol(l,b,h)
END
SUB area(l,b)
a=2*(l+b*h+l*h)
PRINT “Area of box”;a
END SUB
16. FUNCTION Procedure
• A function block statement is used to create a
function procedure to return a calculated
value to a program.
17. Features of FUNCTION Procedure
a. FUNCTION procedure are used in expressions
and can directly return a single value.
b. FUNCTION procedure can be recursive. A
recursive function is one that calls itself.
c. FUNCTION procedure have a data type which
is the return value of the function.
d. The parameters can be passed by reference
or by value.
18. Example
Q. Write a program to declare a user defined
function using FUNCTION…..END FUNCTION to
print the area of a triangle by using expression
method.
Ans. DECLARE FUNCTION area(b!,h!)
INPUT”Enter a base of a triangle:”;b
INPUT”Enter a height of a triangle:”;h
triangle=area(b,h)
PRINT “Area of a triangle=”;triangle
END
20. The difference between Function
procedure and Sub procedure
Sub Procedure Function Procedure
The block of statements is placed with a
pair of SUB/END SUB statements and can
be called by its name
The block of statements is placed with a
pair of FUNCTION/END FUNCTION
statements and can be invoked from the
reference of the function name.
It does not return any value. It directly returns a single value.
It does not have a data type. It has a data type which is the return
value of the function.
21. What are parameters?
Parameters are variables that receives data
(argument values) and to the procedures
(SUB/FUNCTIO N procedures).