Modular Programming-Introduction

Modular programming.

Modular programming is the process of subdividing a program into separate sub-programs. Each modular program has one main module and may have other sub modules and sub module of a program is also called as procedure. Similar functions are grouped in the same unit of programming code and separate function are developed as separate units of code so that the code can be reused by other application.

Advantages of Modular programming.

  1. Easy for developers: They can write block of programs independently
  2. Reduce redundancy: Same modules can used in different place at any time.
  3. Save time: Library can be created so that it can be reused.
  4. Easy to debug: As its written in block of code its easy to debug.
  5. Increase readability: It is easier to interpret small blocks of code that perform a particular task.
Why QBasic is called modular programming?

The program in QBasic can  divide the program into manageable and functional modules with the help of sub procedure and function procedure. QBasic program consists of module-level code and number of procedures. The module level code is the main program controlling the computer. Procedures are separate blocks of statements used by module level code and they can be called by the module-level code any number of times.

SUB Procedure and its features

SUB Procedure is a program designed to perform a specific task. SUB procedure breaks down a program into logical steps which makes program code easier to understand. A CALL statement us used to call the sub procedure module. Its written with SUB……END SUB statements.

Click Here to Check Examples of SUB Procedure

Features:

  1. It does not return any value.
  2. It starts with the key word, SUB and Ends with the keyword END SUB.
  3. It does not have a data type.
  4. It cannot be a part of an expression.
  5. The parameters can be passed by reference or by value.
Function Procedure and its features.

A function procedure is used to define a new function. Its written with Function …..END Function statements.

Features:

  1. It returns single value to call program.
  2. It starts with keyword, FUNCTION and ends with keyword END FUNCTION.
  3. The data type can be a part of expression.
  4. FUNCTION procedure can be recursive (A recursive function is one which calls itself)
  5. The parameter can be passed by reference or by value.

Modular programming

Arguments and Parameters.

An argument is a value that is passed to a function when it is invoked.

A parameter is a variable defined by a function that receives a value when function is called.

Example:

DECLARE SUB test (a, b)

CLS

INPUT “ENTER FIRST NUMBER”; a

INPUT “ENTER SECOND NUMBER”; b

CALL test (a,b)

END

SUB test (a,b)

SUM = a+b

PRINT “The sum is”; SUM

END SUB

Note: Press F2 to switch between Main Module and Sub module.

Passing argument mechanism

a) Arguments passing by reference

When arguments are passed to a procedure by reference the called procedure has access to the memory location of the argument variable. It gives procedure access to the actual variable, the  address of each variable is passed to the procedure and the changes made in the procedure variable will be affect the value of arguments. It is default mode of passing the variable.

CALL TEST (A,B)

b) Argument passing by value

A procedure accepts arguments by value , thus making a copy of the document available to the called procedure.  Arguments passed by value method does not make any affect to value of the variable which are passed to procedure even they are changed in the procedure. To pass arguments by this method, each arguments are enclosed in individual parenthesis in the calling statements.

CALL TEST((A),(B))

Example:

DECLARE SUB rev(n!)

INPUT “Enter any number”; n

PRINT “Before calling sub-program”; n

rev n

PRINT “After calling sub-program”; n

END

SUB rev (n)

WHILE n <> 0

r = n MOD 10

s = s * 10 + r

n = n \ 10

WEND

PRINT “Reversed”; s

END SUB