Thursday, April 9, 2020

Chapter - 10 Modular Programming in QBASIC


The programming technique, in which a large and complex program is divided into small logical and manageable part of the program which can perform specific task, is known as modular programming. The small, logical and manageable part of the program is called procedure (module). Since the modular programming uses small block of functional codes, it is also called structured program.
Advantages of modular programming

A procedure can be reused in a program which reduces the length of the program.
It is suitable for a team work so that task can be divided with a team members.
The debugging of the program becomes easier and faster since they are divided into different modules.
The procedure can be tested and debugged separately.
The documentation of an individual procedure is simpler as compared to the documentation of a large and complex program.
Types of procedure
1. 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 to the main module. A sub procedure can call another sub procedure in the program. A sub procedure is called by using CALL statement. When a sub procedure is called the program control transfers 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 codes the program control returns to the next statement of the calling module i.e. after the CALL statement.

2. Function procedure
A Function procedure is a small, logical and functional part of a program which performs the specific tasks and it returns a single value to the main module or calling module. The returned value of the function procedure may be string or number. So, there are two types of user defined functions. They are String Function and Numeric Function. Once the function procedures are defined in the program they can be used in the program. To use the function procedures in a program, the procedure needs to be called from the main module. The PRINT Statement or a variable is used to call the function procedure.



REM to display volume of cylinder
DECLARE SUB Volume ( r, h)
CLS
CONST pi=22/7
INPUT “Enter radius of a cylinder”; r
INPUT “Enter height of the cylinder”;h
CALL Volume (r, h)
END

SUB Volume (r, h)
          v = pi*r^2*h
          PRINT “Volume of the cylinder =”; v
END SUB



REM checks whether input integer is prime or composite.
DECLARE FUCNTION PRIME$(N)
INTPUT “Enter an integer”; N
PRINT “The number is  :” PRIME$(N)
END
FUNCTION PRIME$(N)
FOR P= 1 TO N
          IF N MOD P = 0 THEN C= C + 1
NEXT P
IF C=2 THEN
          PRIME$= “Prime”
ELSE
          PRIME$=”Composite”
END IF
END FUNCTION



REM to display longest student name among any ten names
DECLARE FUNCTION Longest$(n$( ))
CLS
DIM n$(10)
FOR P= 1 TO 10
          INPUT “Enter student name”;n$(P)
NEXT P
PRINT “The longest student name is “; Longest$(n$( ) )
END

Function Longest$(n$())
long$= n$(1)
FOR P = 2 TO 10
          IF LEN (n$(P))>LEN (l$) THEN long$= n$(P)
NEXT P
Longest$= long$
END FUNCTION

REM to display the number in an ascending order using an array with sub procedure.
DECLARE SUB SORT ( N( ) )
CLS
DIM N(10)
PRINT “Enter ten different numbers:”
FOR P= 1 TO 10
          INPUT N (P)
NEX P
CALL SORT (N ( ))
END



SUB SORT (N ( ))
FOR P= 1 TO 9
          FOR Q = 1 TO 10-P
                    IF N (Q)>N (Q+1) THEN SWAP N (Q), N (Q+1)
          NEXT Q
NEXT P
PRINT “Numbers are in ascending order:”
FOR P = 1 TO 10
          PRINT N (P)
NEXT P
END SUB

REM uses of global variable
DECLARE SUB Average ( )
COMMON SHARED a,b,c
CLS
INPUT “Enter three numbers “; a,b,c
CALL Average
END

SUB Average
          Avg=(a+b+c)/3
          PRINT “average= “;Avg
END SUB

Note: In the above program a,b,c are global variables and Avg is a local variable.



No comments:

Post a Comment