Friday, August 9, 2024

(3.1) Programming in QBASIC - Modular programming

 

3. Programming in QBASIC

(3.1) Programming in QBASIC

 

Modular programming

Modular programming
Modular programming is a technique which divides program into many small logical, manageable and functional modules or blocks.

 

Advantages of modular programming

v Coding the program and testing is very easy.

v Different programmers can write different modules separately.

v Debugging of the program becomes easier and faster.

v Possible to use single module in different places.

 

Module

A module is a block of statement that solves particular problem.

 

Main Module

The top-level module is called main module which is located at the top of all procedures such as sub procedure or function procedure.

 

Sub module

Sub-module is a program which is written under main module.

 

Procedure / Sub Routine
A procedure is a block of statement that solves a particular problem given by user.

Types of Procedure in Modular Programming

v Sub-procedure

v Function-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. 

 

Features of sub procedure

v It is called by using CALL statement.

v It cannot be used as a variable.

v It doesn't return value to the calling module.

v Sub procedure name cannot have type declaration sign.

 

FUNCTION procedure

A function-procedure is a small manageable and functional part of a program that performs specific tasks and returns a value to the calling module. 

 

Features of function procedure

v It returns value to the calling module.

v Function procedure name have type declaration sign.

v A function name can be used as an expression.

v Variables used inside a sub program are local by default.

 

Difference between sub and function procedure.

Sub procedure

Function procedure

Sub procedure name cannot have type declaration sign

Function procedure name not have type declaration sign

Sub procedure does not return value to calling module

Function procedure returns value to the calling module

                       

DECLARE statement

The DECLARE statement is a non- executable statement that declares references to QBASIC sub-program.

 

CALL statement

The CALL statement is a statement that transfers control to another sub procedure.

 

Which statement is used to call sub-procedure?

CALL statement is used to call sub-procedure.

 

Formal parameter (Parameter)

Formal parameter are variables which are used to specify or declare types of data to be passed to the procedure either sub or function. A formal parameter is always variable(s).

 

Actual parameter (Real parameter/argument)

Actual parameter is argument which is used to pass real value or data to the procedure.

Actual parameters may be variables or constant values or can also be in the form of expression.

 

What is passing argument by value in QBasic?

Call by value is a method of calling a procedure in which actual data are passed to the calling procedure module. It is done by enclosing each parameter by a separate parenthesis ( ).

 

What is passing argument by reference in QBasic?

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

 

Difference between passing argument by Reference and passing argument by value methods

Passing argument by reference

Passing argument by value

When arguments are passed by reference, the address of the variable are passed to the procedure.

When arguments are passed by value, it makes a duplicate copy of arguments and their values are used directly in the parameter.

The changes made in the procedure’s variable will affect the variables used at calling module.

It does not make any effect on values of variable which are passed to a procedure even they are changing in the procedure.

By default, the value is passed by reference.

To pass the argument by value, variable is enclosed in parenthesis

 

Local Variable

A variable which is defined in a module and is not accessible to any other module is called local variable.

The value of local variable is destroyed when execution of module is over.

Global variable

Variables which are declared outside the procedure and whose values can be accessed from any procedure or module are called global variables.

The value of global variable is not destroyed during the execution of program.

DIM SHARED, COMMON SHARED and SHARED statement is used to declare global variable.

DIM SHARED statement

The DIM SHARED statement makes the variable accessible or global to all the modules. It is used in the main module.

 

COMMON SHARED statement

The COMMON SHARED statement is a non executable statement that declares variables as global, so that they can be shared between main module and sub modules. It appears only in the main module.

SHARED

The SHARED statement is used to share variables or part of a module among a module without making the variables global. It appears only in the sub module.

 

String function [SEE 2074 U]

String function is used with string it returns string value.

 

Numeric function [SEE 2074 U]

Numeric function is used with number it returns numeric value.

 

Library functions / Built-in Function [SQE 2074K]
Library functions are built-in or readymade functions provided by QBASIC. E.g. MID$( ), LEN( ), SQR( ) etc.
LEN ( ) function returns the number of characters in a string or the number of bytes required to store a variable.

User defined function
Function which is defined by the user according to the need is called user defined function.

 

 


 

No comments:

Post a Comment