Modular Programming In QBASIC Class 10 Computer Science Notes

 

Modular Programming In QBASIC Class 10 Computer Science Notes

Modular Programming In QBASIC Class 10 Computer Science Notes
Modular Programming In QBASIC Class 10 Computer Science Notes


Concept of modular programming

A program which can perform many different tasks includes more number of statements. As the number of statements in the program increases, it becomes more complex and unmanageable. To make a program simple and manageable we need to break down the program into smaller parts continuously till the small block of program becomes simple and easy to manage. The programming technique in which program is divided into smaller logical and manageable part is known as modular programming. The small, logical and manageable part of the program is called procedure or module. Since the modular programming uses small block of functional codes, it is also called structured programming.

 

Advantages of procedure

  1. A procedure can be reused in a program more than once without rewriting it. So, it reduces the length of the program.
  2. It is suitable for team work since one member could write a part of the program whereas another part of the program can be written by other members.
  3. Debugging of the program becomes easier and faster since they are divided into different models.
  4. The procedure can be tested and debugged separately.
  5. The documentation of an individual procedure is more simple as compared to the other documentation of a large program.
  6. It improves the readability of a program.

 

Main Module and Sub Module

A modular program has a main module and may have many sub modules (i.e. procedures). The main modules contain the entry point and the ending point of the program. The statement or codes returned a main module are known as Module Level codes.

A small, logical and manageable functional part of the program is known as procedure. A process which is also known as a sub program or sub module contains codes for performing a specific task. The statement or codes of a procedure are known as Procedure Level codes. Each procedure is identified by the name assigned to it. The procedure naming follows the process for naming of variables. A procedure is needed to call from the main module to use it. A procedure can be called from another procedure too. When you call a procedure by referring its name, the program control transfers from the calling module to the called module and after the completion of task in the module the program control returns back to the calling module.

 

Types of Procedure

There are two types of procedures: They are:

·               SUB procedure

·               FUNCTION procedure

 

SUB Procedure

A sub procedure is a small logical and manageable functional part of a program which performs the specific tasks and does not return any value. Once the sub procedures are defined, the can be used in the program. To use them they or needed to call from the main module. A sub procedure can call another sub procedure in the program too. A sub procedure is called by using CALL statement. When a sub procedure is called the program control transfer from the main module to the sub module and the execution of the codes in the sub module takes place. After the completion of execution of its code the program control returns to the next statement of the calling module i.e. after the CALL statement.

 

 Function Procedure

 A function procedure is more logical and functional part of a program which performs the specific tasks and returns a single value to the main program or calling module. The returned value of the function procedure may be string or number. So there are two types of user defined functions and they are:

·               String function

·               Numeric function

Once the function procedures are defined in the program they can be used in the program. To use the function procedures in the program the procedure it needs to be called from the main module.

 

Declaring a procedure

In a modular programming, the Sub modules or procedure which are to be used in a program or needed to declare first in the program. The declaration of a procedure specifies a procedure name and parameters that are used in the procedure. To declare a procedure in a program, DECLARE statement is used. The DECLARE statement declares a procedure’s name, number of parameters and types of parameters that are needed to define the procedure. The DECLARE statement checks the number and types of parameters defined in the procedure with number and types of arguments that are passed to the procedure.

 

 Syntax:

         DECLARE SUB/FUNCTION NAME (parameterList)

Where,

NAME= The name of a procedure

ParameterList= A list of variables defined in a procedure which refers the number and types of arguments.

 

Example

DECLARE SUB Test (a, b)

It declares a sub procedure named ‘Test’ having two numeric variables: a and b

 

DECLARE FUNCTION CHECK$(W$)

It declares a function procedure named ‘CHECK$’ having a string variable W$.

 

DECLARE SUB HCF (N AS INTEGER, M AS INTEGER)

It declares a sub procedure named ‘HCF’ having two integer parameters: N and M

 

 

 Note:

  1. If you do not include the declaration of a procedure in a program, then QBASIC automatically adds a line at the beginning of the program when you save the program.
  2. The declaration line declares a procedure so that the main program knows that the procedure will be used.

 

 Defining SUB Procedure

 After the declaration of a procedure, the procedure needs to be defined in a program. The SUB….END SUB statement defines a procedure. The defining of sub procedure needs to be done according to the declaration of the procedure. The number and types of parameters must be same as in the declaration of the procedure.

 Syntax:

         SUB name (parameterlist)

         Statements

         END SUB

 

 Where,

  1. Name is the name of the sub procedure.  The sub procedure name follows the rule of naming a variable but it does not have the data type suffix like $, %, &, !, or, #
  2. Parameterlist is the list of variables separated by comma. Each parameter accepts a constant or a reference value of a variable that is passed to it from the calling module in the sequence order.

 

When you type SUB along with the name and parameters after the END statement in the main module, it opens a new editing window including SUB with name and parameters and END SUB. In between SUB and END SUB you have to enter the required code. For example, when you type SUB test (a, b) after the end statement in the main module, you will see a new window where the necessary codes are entered.


Modular Programming In QBASIC Class 10 Computer Science Notes


 

 Note:

  1.  A sub procedure does not return any value to the calling module.
  2.  Sub procedure name does not have a data type so it does not contain any type declaration sign.
  3. Sub procedure name can't be used in an expression.
  4. Exit sub statement can be used to terminate a sub procedure.
  5. Sub procedure is called by CALL statement.
  6. Sub procedure can be called from the same procedure that is a procedure can be recursive.
  7. Switching from sub procedure module is done by pressing F2 key or choosing view Subs from view menu and selecting the main module or other sub module. Use this View Subs Window to select the procedure that you want to edit and delete.
  8. If you want to move from one module to another module without selecting module, use SHIFT+ F2 keys.

 

Calling a SUB procedure

Once the sub procedure is defined it can be used in the program by calling it using CALL statement. The CALL statement is used to execute a sub procedure. When a sub procedure is called using CALL statement the program control transfers from the calling module to the sub procedure. After the execution of the statement in the procedure the program control returns to the calling statement next to the CALL statement.

 

Syntax:

         CALL name(argumentlist)

Where,

Name id the name of SUB procedure to be called.

Argumentlist is a list of variables or constants separated by commas and are passed to a SUB procedure.

 

Examples:

CALL Test (a, b)

It executes ‘Test’ sub procedure by passing two arguments.

 

CALL HCF (N AS INTEGER, M AS INTEGER)

It calls ‘HCF’ sub procedure by supplying two integer arguments.

 

Notes:

  1. The number of arguments and the number of parameters should be same and of the same type.
  2.  When no data has to be passed to a sub procedure then no parenthesis is required after the procedure name in the calling statement.
  3. If you omit the CALL keyword you have to omit the parenthesis around argument list. In such case you need to declare the procedure in a DECLARE statement before calling it, or save the program and QBASIC automatically adds a DECLARE statement.

For example:

CALL Check (N) can be written as Check N

 

ARGUMENT AND PARAMETER

When a procedure is called from the main module, the data or variables may need to be supplied to the procedure where they are needed to use. The constants or variables enclosed in the parenthesis are supplied to a procedure are known as arguments. The argument is also known as actual parameter. The variables in the sub module or a procedure which accept data or variables passed to them from the calling module are known as parameters. Parameter is also known as formal parameter. 

Modular Programming In QBASIC Class 10 Computer Science Notes



Arguments can be passed to procedure by Reference or by Value method.

 

 Passing Arguments by Reference

 The default argument passing is by reference. In the Reference method the addresses of the variables are passed to the procedure. That means a variable of the procedure (i.e. parameter) uses the memory location of the argument which is passed to it. So if the value of the variable is changed in the procedure then it changes the value of the variable in the main module too. For example, if you let your motorbike having 10 liters of petrol in its fuel tank to share somebody, then certainly the amount of the fuel in the tank reduces after riding 40 km. So, when you get your motorbike back you find there is change in the amount of fuel. Since the argument and the corresponding parameters both uses the same memory location in the by Reference method any changes in the value of the variable has done in the procedure makes changes in the value of a variable in the main module. 

 

Passing Arguments by Value

When you pass arguments by Value method it does not make any effect to the value of the variables which are passed to a procedure even if they are changed in the procedure. Passing arguments by Value method is useful when you have to pass the same value to the more than one procedure keeping the original value unaffected. When arguments are passed by Value method, it makes the duplicate copy of arguments and their values (i.e. constants) of arguments, if the values of parameters are changed in the procedure they do not make changes in the value of arguments. To pass arguments by value method each argument is enclosed in individual parenthesis in the calling statement.

 

For Example:

CALL TEST ((A), (B), (C$))

Here values of A, B, and C$ are passed by Value method.

 

Examples:

This program finds the sum of two numbers

DECLARE SUB SUM (A, B)

CLS

INPUT “ENTER FIRST NUMBER”; A

INPUT “ENTER SECOND NUMBER”; B

CALL SUM (A, B)

END

SUB SUM (A, B)

S = A + B

PRINT “SUM OF TWO NUMBERS”; S

END SUB

 

This program finds the area of rectangle.

DECLARE SUB AREA (L, B)

CLS

INPUT “ENTER LENGTH”; L

INPUT “ENTER BREADTH”; B

CALL AREA (L, B)

END

 

SUB AREA (L, B)

A = L * B

PRINT “AREA OF RECTANGLE”; A

END SUB

 

This program finds the area of a circle.

DECLARE SUB AREA(R)
CLS
INPUT “Enter radius”; R
CALL AREA(R)
END
SUB AREA(R)
A= (22/7) * R^2
PRINT “The area of circle=”; A
END SUB

 

This program finds the average of three numbers.

DECLARE SUB Average (a, b, c)

CLS

INPUT” Enter the first number”; a

INPUT” Enter the second number”; b

INPUT” Enter the third number”; c

CALL Average (a, b, c)

END

SUB Average (a, b, c)

A= (a + b + c)/3

PRINT " The average is "; A

END SUB

 

This program checks whether a number is completely divisible by 13 or not.

DECLARE SUB CHECK(N)
CLS
INPUT” ENTER A NUMBER”; N
CALL CHECK(N)
END
SUB CHECK(N)
IF N MOD 13=0 THEN
PRINT “THE NUMBER IS COMPLETELY DIVISIBLE BY 13”
ELSE
PRINT” THE NUMBER IS NOT COMPLETELY DIVISIBLE BY 13”
END IF
END

 

This program finds the area of 4 walls.

DECLARE SUB AREA (L, B, H)
CLS            
INPUT “ENTER LENGTH”; L            
INPUT “ENTER BREADTH”; B            
INPUT “ENTER HEIGHT”; H            
CALL AREA (L, B, H)            
END            
SUB AREA (L, B, H)            
A = 2 * H * (L + B)            
PRINT “AREA OF FOUR WALLS =”; A            
END SUB 

 

This program prints the Fibonacci series as 1 1 2 3 5 … up to 10th terms

DECLARE SUB SERIES ( )
CLS
CALL SERIES
END
SUB SERIES ( )
A = 0
B = 1
FOR I = 1 TO 10
C = A + B
PRINT C;
A = B
B = C
NEXT I
END SUB

 

This programs prints the reverse of the entered word

DECLARE SUB REVERSE (W$)
CLS
INPUT “Enter any word”; W$
CALL REVERSE(N$)
END
SUB REVERSE (W$)
FOR I = LEN(W$)
C$ = MID$(W$, I, 1)
R$=R$+C$
NEXT I
PRINT “The word in reverse order is “; R$
END SUB
  
 

 

 

Function Procedure

A function procedure performs a specific task and it returns a value to the main program. To use the function procedure, it is needed to call. Since a function procedure returns of value, it is either called by using PRINT statement or storing a returned value into a variable.

 

 Defining a Function Procedure

 Once a function procedure is declared, the function procedure (i.e. user-defined function) can be defined in the program. A function procedure is defined by using the FUNCTION…END FUNCTION statement.

Syntax:

         FUNCTION Name (parameterlist)

         Statements

         Name= Expression

         END FUNCTION 

Where,

  1. Name is the name of function procedure. The rule for naming a function is the same as rules for naming a variable. Since a function returns a value, a function may be string or a numeric. A string function name is followed by the type declaration symbol dollar ($) whereas numeric function name may be followed by either %, &, !, or # symbol. 
  2. Parameterlist is the list of variables that accepts the values passed to it when the function is called. Each variable or constant is separated by commas.
  3. Expression is a value that has to be returned to the calling module. 

 Note:

  1.  A function returns either a string value or a numeric value. If the returned value will be number, then you need to define a function or otherwise you need to define a string function.
  2. A function name can be used in an expression.
  3. A function procedure can be recursive.
  4. The EXIT FUNCTION statement provides an alternative exit from a FUNCTION 

Whenever you type keyword Function with the name and parameter after the END statement in the main module, then it opens new Edit Window where you can type necessary codes in between in the between FUNCTION and END FUNCTION.

For example, when you type,

FUNCTION Greatest$(a, b, c)

And as soon as you press Enter key you will see a new Edit Window.

 

Calling Function Procedure

Once the function procedure is defined, it can be used in the program by calling it. A function procedure can be called from anywhere in the program. The CALL statement is not used to invoke a function procedure. Calling a function procedure is similar to calling a library function. A function procedure can be called using PRINT statement, storing the returned value in a variable or using an expression.

 

Examples:

 

This program finds the sum of two numbers.

DECLARE FUNCTION SUM (A, B)

CLS

INPUT “ENTER FIRST NUMBER”; A

INPUT “ENTER SECOND NUMBER”; B

S = SUM (A, B)

PRINT “SUM OF TWO NUMBERS”; S

END

 

FUNCTION SUM (A, B)

SU = A + B

SUM = SU

END FUNCTION

 

This program finds the product of two numbers

DECLARE FUNCTION PROD (A, B)

CLS

INPUT “ENTER FIRST NUMBER”; A

INPUT “ENTER SECOND NUMBER”; B

PR = PROD (A, B)

PRINT “PRODUCT OF TWO NUMBERS”; PR

END

 

FUNCTION PROD (A, B)

P = A * B

PROD = P

END FUNCTION

 

 

This program finds the area of rectangle.

DECLARE FUNCTION AREA (L, B)

CLS

INPUT “ENTER LENGTH”; L

INPUT “ENTER BREADTH”; B

AR = AREA(L, B)

PRINT “AREA OF RECTANGLE ”; AR

END

 

FUNCTION AREA (L, B)

A = L * B

AREA = A

END FUNCTION

 

This program finds the area of triangle

DECLARE FUNCTION AREA (B, H)
CLS
INPUT “ENTER BASE”; B
INPUT “ENTER HEIGHT”; H
PRINT “AREA OF TRIANGLE”; AREA (B, H)
END
FUNCTION AREA (B, H)
AREA = (1/2) * B * H
END FUNCTION

 

This program finds the average of three numbers.

DECLARE FUNCTION AVG (a, b, c)

CLS
INPUT” Enter the first number”; a
INPUT” Enter the second number”; b
INPUT” Enter the third number”; c
PRINT” The average number is ”; AVG (a, b, c)
END
FUNCTION AVG (a, b, c)
Average= (a + b + c)/3
AVG= Average
END FUNCTION

This program prints the reverse of the word entered in the program.

DECLARE FUNCTION REVERSE$ (S$)
CLS
INPUT "ENTER ANY WORD"; S$
PRINT "REVERSED WORD IS "; REVERSE$(S$)
END
FUNCTION REVERSE$ (S$)
FOR I = LEN(S$) TO 1 STEP -1
B$ = MID$(S$, I, 1)
W$ = W$ + B$
NEXT I
REVERSE$ = W$
END FUNCTION

 

This program counts the number of vowel letters in a given word

DECLARE FUNCTION COUNT (S$)
CLS
INPUT "ENTER ANY STRING"; S$
PRINT "TOTAL NO. OF VOWELS= "; COUNT(S$)
END
FUNCTION COUNT (S$)
C = 0
FOR I = 1 TO LEN(S$)
A$ = MID$(S$, I, 1)
B$ = UCASE$(B$)
IF B$ = "A" OR B$ = "E" OR B$ = "I" OR B$ = "O" OR B$ = "U" THEN
C = C + 1
END IF
NEXT I
COUNT = C
END FUNCTION

 

This program coverts temperature from degree Celsius to degree Fahrenheit

DECLARE FUNCTION TEMP (C)
CLS
INPUT “ENTER TEMPERATURE IN CELCIUS”; C
PRINT “TEMPERATURE IN FAHRENHEIT=”; TEMP (C)
END
FUNCTION TEMP (C)
F = (9/5) * C + 32
TEMP = F
END FUNCTION

 

 

 

Passing Array as an Argument

An array can be passed as an argument to a sub procedure. When you pass an array as an argument, the size of the array should not be specified in the declaration as well as in the calling statement. That means the array name should be followed by an empty set of parentheses.

For example,

         DECLARE SUB CHECK (numner())

Where,

CHECK is the sub procedure name and number is the array

 

Example Program

 

This program passes array as the parameter

DECLARE SUB SORT (NUM())

CLS

DIM NUM (10)

PRINT” Enter ten different numbers”

FOR P = 1 TO 10

INPUT NUM (P)

NEXT P

CALL SORT (NUM ())

END

SUB SORT (NUM ())

         FOR P= 1 TO 9

FOR Q=P+1 TO 10

IF NUM(P)>NUM(Q) THEN SWAP NUM (P), NUM(Q)

NEXT Q

NEXT P

PRINT” Numbers are in ascending order”

FOR P = 1 TO 10

         PRINT NUM (P)

NEXT P

END SUB      

 

 LOCAL AND GLOBAL VARIABLES

Variable is the symbol or character that stores program data.  A variable which is declared inside a module and cannot be accessed by other module is known as a local variable. All variables that you declare either implicitly or explicitly without using SHARED attribute are local variables. The local variable can be used within the module where it is defined. To use the local variables defined in the module to another module the variables must be passed to the module where you want to use them. For example, if a variable is defined in the main module then without passing it as a parameter to a procedure you can’t use it in the procedure. Variable names in the different modules may be same but it does not mean that they all are representing the same data or memory location. 

 

Variable in main module which can be accessed from any sub module or procedure of a program is known as Global variable. A global variable is declared in the main module by using DIM statement or COMMON statement with SHARED attribute. Since the Global variables are available to all procedures in the program they don't need to pass arguments to any procedure.

 

Defining Global variables using COMMON Statement

The COMMON statement with SHARED attribute defines a global variable in a program.

 

Syntax:

         COMMON SHARED VariableList

Where,

SHARED indicates that variables are shared with all procedures.

VaraibleList is a list of one or more variables to be shared in a program.

 

Eg: COMMON SHARED A, B, C

 

Note:

The COMMON statement must be placed before any executable statement in the main module of a program.

 

Defining Global variables using DIM Statement

The DIM statement with SAHRED attribute defines a global variable in a program.

Syntax:

         DIM SHARED Variable

E.g: DIM SHARED W$

 

Example:

DECLARE SUB Temperature ( )

DIM SHARED C

CLS

INPUT” Enter temperature in Celsius”; C

CALL Temperature

END

SUB Temperature

F=9*C/5+32

PRINT” Temperature in Fahrenheit”; F

END SUB

 

The variable ‘C’ defined in the main module of a program is global variable so without passing its value to ‘Temperature’ it can be used in the procedure.

 

Sharing Variables Defined in a Main Module with in a Procedure

You can share variables among parts of a module without making the variables global by using the SHARED statement. The SHARED statement is used only in the sub module.

 

The Syntax of SHARED statement is as below:

         SHARED variable AS type

 

Example:

DECLARE SUB Temperature ( )

CLS

INPUT” Enter temperature in Celcius”; C

CALL Temperature     ‘SUB procedure without parameter’

END

 

SUB Temperature

SHARED C                 ‘makes variable available to the procedure’s

F=9*C/5+32

PRINT” Temperature in Fahrenheit”; F

END SUB

 

 Click here to get QBASIC Related Question Answer

 

  

 

Comments