besidedegree@gmail.com
+9779709005491
Back to Home
School SEE Computer Science

Modular Programming

Highlight Save
Modular programming is the method of dividing a large program into several small, logical and manageable parts called modules. Each module performs a specific task, which makes the whole program easier to design, understand and debug. The main program is known as the main module, and the smaller parts are called sub-modules or procedures. In QBASIC, modular programming is handled through Sub-procedures and Function-procedures. A sub-procedure is written using SUB…END SUB and performs a task without returning a value, while a user-defined function is written using FUNCTION…END FUNCTION and always returns a value. Both require declaration before use and accept parameters to receive data from the main module. Parameters used during declaration and definition are called formal parameters, whereas the real data passed during program execution are called actual parameters. Variables used inside a single module are local variables, while variables shared across modules are global variables, declared using SHARED, DIM SHARED or COMMON SHARED. Modular programming improves readability, reduces errors, allows code reusability and enables many programmers to work independently. This approach helps students and programmers build structured, efficient and easy-to-maintain programs.

Introduction

Programming becomes messy when everything is written in one giant block. To avoid chaos, programmers break a big program into smaller, meaningful pieces.
This idea is called Modular Programming.

In modular programming:

A big program is divided into modules (small blocks).

Each module performs a specific task.

There is always one main module and many optional sub-modules.

A module is also called a procedure.

This makes programs easier to read, understand, test and reuse.

Advantages of Modular Programming

Different programmers can work on different modules at the same time.

Errors are easier to find and fix.

Same module can be used again in other parts of the program.

The program becomes clear and easier to read.

Designing, modifying and updating code becomes simple.

Main Module and Sub-Modules

Main Module: The starting part of the program.

Sub Modules: Smaller blocks created to perform special tasks (like calculating, printing, checking, etc.)

Diagram:

 

     

Modular Programming in QBASIC

QBASIC supports modular programming in 2 ways:

Sub-procedure (SUB…END SUB)

Function-procedure (FUNCTION…END FUNCTION)

A) SUB-PROCEDURE

What is a Sub-procedure?

A sub-procedure is a small part of a program that performs a task but does NOT return any value to the main module.

Written as:

A sub-procedure must be:

Declared

Defined

Called

1. Declaration Part

Before using a sub-procedure, we declare it using DECLARE SUB.

Syntax:

 

2. Definition Part

This is the block where we write the actual code for the sub-procedure.

Syntax:

 

 

3. Calling the Sub-Procedure

We use CALL from the main module.

Syntax:

 

Example:

 

Complete Example (Sum of Two Numbers)

 

Call by Reference and Call by Value

1. Call by Reference (Default)

The memory address of the variable is passed.

Changes in sub-module affect main-module.

Example Output: 15 10

2. Call by Value

Only the value is sent.

Changes inside sub-module DO NOT affect main module.

Done by using double parentheses.

Example Output: 5

Formal Parameter vs Actual Parameter

Formal Parameters:
Variables used in the DECLARE and SUB/FUNCTION definition.

Actual Parameters:
Real values passed during CALL (from main program).

Local and Global Variables

Local Variable

Declared inside a module.

Only works inside that module.

Global Variable

Can be used in all modules.

Declared using:

COMMON SHARED (main module)

DIM SHARED (main module)

SHARED (inside sub-module)

Examples of Sub-Procedures

Area of Circle

Multiplication Table

Odd/Even Check

First 10 Natural Numbers

Palindrome Check

Area and Volume of Room

(These are already included above; rewritten explanations available if needed.)

B) FUNCTION-PROCEDURE

Functions are like small machines that:

Take input (parameters)

Perform calculation

Always return a value

Written as:

Declaration of Function

Calling a Function

Two ways:

1. Assign to variable

 

2. Print directly

 

Example (Area of Rectangle)

Examples of Function Procedures

Simple Interest

Circumference

Smallest Number

Vowel Count

Sum of Digits

Reverse String

Area and Volume

Summary (Exam-Focused Points)

Modular programming means writing programs in multiple modules.

Two kinds of modules in QBASIC:

Sub-Procedure (no return value)

Function-Procedure (returns value)

DECLARE SUB and CALL are used for sub-procedures.

DECLARE FUNCTION and assignment/PRINT are used for functions.

Sub-procedure uses SUB…END SUB.

Function uses FUNCTION…END FUNCTION.

Formal parameters: used during declaration/definition.

Actual parameters: used during CALL.

Local variables: work only inside the module they are declared.

Global variables: accessible from all modules using SHARED.

Related Videos

Modular Technology by Information Technology