Modular Programming

Discover Modular Programming in QBASIC

Table of Contents

Introduction

Modular programming is an approach to programming where a program is divided into smaller programs or sub-programs. QBASIC supports modular programming in which a program is divided into the main module and its sub-modules (also known as procedures) where each sub-module has its own name and independent functionality.

Types of procedures

There are two types of procedures supported by QBASIC; SUB procedure and FUNCTION procedure. Both of these are small modules that act independently. The only difference is SUB procedures do not return and value while the FUNCTION procedures must return a value to the main module. Each sub-procedure needs to be closed once opened using the END statement. Below are examples of a simple program written using SUB and FUNCTION procedure to add two numbers.

View All: QBASIC Programs

SUB Procedure:

REM program to add two numbers using SUB procedure
DECLARE SUB add
CLS
CALL add
END
SUB add
a = 10
b = 20
sum = a + b
PRINT "The sum is ";sum
END SUB

FUNCTION Procedure

REM program to add two numbers using FUNCTION procedure
DECLARE FUNCTION add
CLS
PRINT add
END
FUNCTION add
a = 10
b = 10
sum = a + b
add = sum
END FUNCTION

Explanation

Looking at the examples above, we can notice that while writing the programs using SUB and FUNCTION procedure, first the modules are declared. Declaration of sub-procedures has a particular syntax i.e., DECLARE statement followed by the type of procedure (SUB or FUNCTION) and finally, the name of the procedure with or without parameters in parentheses. In the main module, the sub-module is called. The methods of calling the modules are different. In SUB procedure, the CALL statement is used to call the function while in FUNCTION procedure, PRINT statement or receiving the returned value in a variable can be used.

Inside the module, the input and calculation are done. In the SUB procedure, we can simply print the output while in the FUNCTION procedure, we need to return the desired output by passing the output to the name of the procedure i.e., add = sum in the above program. Here, add is the name of the procedure and sum is the desired output.

Arguments and Parameters

These programs are very simple with no use of parameters and arguments. These programs can be written using parameters and arguments. First, let’s understand the arguments and parameters. Arguments are the variables or constants enclosed in the parentheses of the calling statement while parameters are the variables used to accept the values of the arguments.

Example of a program using arguments and parameters:

REM program to add two numbers by passing arguments and parameters 
DECLARE SUB add(a, b)
CLS
x = 10
y = 20
CALL add(x, y)
END
SUB add (p, q)
sum = p + q
PRINT "The sum is ";sum
END SUB

Explanation

In the above example, the variables (x and y) used while calling the add SUB procedure are arguments. The variables p and q are parameters since these variables accept the values of the arguments x and y and are used to perform calculations (notice sum = p + q, and not sum = x + y). Parameters are used for calculations inside the SUB module while the arguments are the variables with some values in the main module that are passed to the sub-modules.

Also, notice the variables a and b used in the declaration statement. These are also parameters but don’t get confused for they are not used anywhere in the program. These parameters and known as formal parameters; used while declaring the sub-procedures to inform the QBASIC interpreter to expect two parameters in the sub-module. Likewise, the parameters p and q are known as actual parameters as they are actually used in the sub-procedures.

Leave a Comment

Your email address will not be published. Required fields are marked *

Exit mobile version