Modular programming
a. What is modular programming?
Modular
programming is a programming technique in which a program is divided into
smaller, manageable, and independent blocks or modules (such as procedures and
functions). Each module performs a specific task and can be reused in other
programs.
b.
Write any two advantages of modular programming.
Code
Reusability – Modules can be reused in different programs without rewriting the
code.
Easy
Debugging and Maintenance – Errors can be easily traced and corrected within
specific modules.
c.
List the types of parameter.
Formal
Parameter
Actual
Parameter
d.
What is formal parameter?
A
formal parameter is a variable used in a procedure or function declaration that
receives the value passed by the calling program. It acts as a placeholder.
Example:
SUB
Add(a, b) …………….. here a and b are formal parameters
e.
What is actual parameter?
An
actual parameter is the real value or variable passed to a procedure or
function when it is called.
Example:
CALL
Add(5, 10) ……………………….5 and 10 are
actual parameters
f.
What is local variable?
A
local variable is a variable that is declared and accessible only within a
specific procedure or function. It cannot be accessed outside that procedure.
g.
Differentiate between SUB procedure and FUNCTION procedure.
SUB Procedure |
FUNCTION Procedure |
Does not return a value directly. |
Returns a single value. |
Called using CALL statement. |
Called by using its name in an expression. |
h.
What is global variable?
A
global variable is a variable that can be accessed from all procedures and
functions in the program. It is declared using COMMON SHARED or SHARED.
i.
What is procedure? List its type.
A
procedure is a block of code designed to perform a specific task.
Types of procedures:
SUB
Procedure
FUNCTION
Procedure
j.
Differentiate between argument pass by value and argument pass by reference.
Pass by Value |
Pass by Reference |
Only a copy of the actual parameter is passed. |
The address of the actual parameter is passed. |
Changes made do not affect the original variable. |
Changes made do affect the original variable. |
k.
Write the function of the following statements:
i.
CALL – Used to invoke a SUB procedure.
Example: CALL Display()
ii.
DECLARE – Declares the signature of a SUB or FUNCTION before its use.
Example: DECLARE SUB Display()
iii.
COMMON SHARED – Declares global variables shared across multiple modules.
Example: COMMON SHARED x, y
iv.
SHARED – Declares a variable within a procedure that can be accessed globally.
Example: SHARED count
6. Write a Qbasic Program that
ask length and breadth of a room and calculate area and perimeter. Create
a user defined function to calculate area and sub program to calculate
perimeter. [HINT a=lXB P=2(L+B)]
DECLARE FUNCTION AREA(L,B)
DECLARE SUB PERIMETER
(L,B)
CLS
INPUT “ENTER LENGTH
AND BREADTH”;L,B
PRINT “AREA OF
ROOM=”; AREA(L,B)
CALL PERIMETER (L,B)
END
FUNCTION AREA(L,B)
AREA=L*B
END FUNCTION
SUB PERIMETER (L,B)
P=2*(L+B)
PRINT “PERIMETER OF
ROOM=”; P
END SUB
7. Write a Qbasic Program that ask length, breadth
and height of a room then use sub procedure to calculate volume and function
procedure to calculate area of four walls.
DECLARE SUB VOLUME
(L,B,H)
DECLARE FUNCTION
AREA(L,B.H)
CLS
INPUT “ENTER
LENGTH, BREADTH AND HEIGHT”;L,B,H
CALL VOLUME (L,B,H)
PRINT “AREA OF
ROOM=”; AREA(L,B,H)
END
SUB VOLUME (L,B,H)
V=L*B*H
PRINT “VOLUME OF
ROOM=”; V
END SUB
FUNCTION AREA(L,B,H)
AREA=2*(L+B)*H
END FUNCTION
8. Write a Program to calculate area of circle
using function procedure and use sub procedure to calculate circumference in
Qbasic. [Hint
a=πr2 c=2πr]
DECLARE FUNCTION
AREA(R)
DECLARE SUB
CIRCUMFERENCE (R)
CLS
INPUT “ENTER
RADIUS”;R
PRINT “AREA OF
CIRCLE=”; AREA(R)
CALL CIRCUMFERENCE
(R)
END
FUNCTION AREA(R)
AREA=(22/7)*R^2
END FUNCTION
SUB CIRCUMFERENCE
(R)
C=2*(22/7)*R
PRINT
“CIRCUMFERENCE OF CIRCLE=”; C
END SUB
9. Write a program in QBASIC that asks two numbers to
find square of two numbers using SUB procedure and sum of two numbers
using FUNCTION procedure.
DECLARE SUB SQUARE
(A,B)
DECLARE FUNCTION
SUM (A,B)
CLS
INPUT “ENTER TWO
NUMBERS”;A,B
CALL SQUARE
(A,B)
PRINT
“SUM OF TWO NUMBERS =”; SUM (A,B)
END
SUB SQUARE
(A,B)
S1=A^2
S2=B^2
PRINT
A;B;“SQUARE OF THESE NUMBERS ARE”;S1,S2
END SUB
FUNCTION SUM (A,B)
SUM =A+B
END FUNCTION
10.Write Qbasic program to find average of three numbers
by using FUNCTION...END FUNCTION and find greatest number by using SUB
procedure.
DECLARE FUNCTION
AVG(A,B,C)
DECLARE SUB
GREATEST(A,B,C)
CLS
INPUT “ENTER THREE
NUMBERS”;A,B,C
CALL GREATEST(A,B,C)
PRINT “AVERAGE OF
THREE NO.=”; AVG(A,B,C)
END
FUNCTION AVG(A,B,C)
AVG=(A+B+C)/3
END FUNCTION
SUB GREATEST(A,B,C)
IF A>B AND
A>C THEN
PRINT A;“IS GREATEST”
ELSEIF B>A AND B>C THEN
PRINT B;“IS GREATEST”
ELSE
PRINT C;“IS GREATEST”
END IF
END SUB
11.Write a program to define a function procedure to
display area of sphere and sub procedure to display volume of sphere where user
has to input the required data in the main module. [Hint: Area of sphere: 4
xPIxR^2, Volume- V=4/3XPIxR^3
DECLARE FUNCTION AREA(R)
DECLARE SUB
VOLUME(R)
CLS
INPUT “ENTER
RADIUS”;R
PRINT “ area
of sphere=”; AREA(R)
CALL VOLUME(R)
END
FUNCTION AREA(L,B)
AREA=4 *(22/7)*R^2
END FUNCTION
SUB VOLUME (R)
V=(4/3)*(22/7)*R^3
PRINT “VOLUME OF
SPHERE=”; V
END SUB
12.Write Qbasic program to find smallest number among three numbers by using FUNCTION...END FUNCTION and find greatest number by using SUB procedure.
DECLARE FUNCTION SMALLEST(A,B,C)
DECLARE SUB GREATEST(A,B,C)
CLS
INPUT “ENTER THREE NUMBERS”;A,B,C
CALL GREATEST(A,B,C)
PRINT “SMALLEST OF THREE NO.=”; SMALLEST(A,B,C)
END
FUNCTION SMALLEST (A,B,C)
IF A<B AND A<C THEN
ELSEIF B<A AND B<C THEN
ELSE
END IF
END FUNCTION
SUB GREATEST(A,B,C)
IF A>B AND A>C THEN
PRINT A;“IS GREATEST”
ELSEIF B>A AND B>C THEN
PRINT B;“IS GREATEST”
ELSE
PRINT C;“IS GREATEST”
END IF
END SUB
DECLARE FUNCTION SMALLEST(A,B,C)
DECLARE SUB GREATEST(A,B,C)
CLS
INPUT “ENTER THREE NUMBERS”;A,B,C
CALL GREATEST(A,B,C)
PRINT “SMALLEST OF THREE NO.=”; SMALLEST(A,B,C)
END
FUNCTION SMALLEST (A,B,C)
IF A<B AND A<C THEN
ELSEIF B<A AND B<C THEN
ELSE
END IF
END FUNCTION
SUB GREATEST(A,B,C)
IF A>B AND A>C THEN
PRINT A;“IS GREATEST”
ELSEIF B>A AND B>C THEN
PRINT B;“IS GREATEST”
ELSE
PRINT C;“IS GREATEST”
END IF
END SUB
Model Question
Chapter 8: Modular Programming in Qbasic
1.What is Modular Programming?
2 What is Procedure? Mention its types
3 Write the difference between sub-procedure and function-procedure
4 Define main-module
5 Differentiate between local and global variable.
6 Write anyone QBASIC program to show the difference between call try reference and call by value
7. Write a program in QBASIC that asks length, breadth of a room and calculate its area and perimeter. Create a user defined function to calculate area and sub program to calculate perimeter. [Hint: [Area=LxB], [p=2(L+B)]
8. Write a program in QBASIC that will asks the user to input length, breadth and height of a room then use SUB procedure calculate its volume and FUNCTION procedure to calculate its area of four walls.
9. Write Qbasic program to find average of three numbers by using FUNCTION. END FUNCTION and find greatest number by using SUB procedure.
10.Write a program to calculate Area of circle using Function procedure and use SUB procedure to calculate as circumference in Q-Basic. (Hint: [Ar2], [C=2]
11.Write a program in QBASIC that ask the radius of circle. Write a program to calculate the area and circumference of a circle Create a user defined function first (r) to calculate area and sub procedure second (r) to calculate circumference of a circle.
12.Write a program in QBASIC that asks length, breadth and height of room and calculate its area and volume. Create a user defined function to calculate area and sub-program to calculate volume. (Hint: [A-L-B], [V=LxBxH]
13.Write a program in OBASIC that will asks the user to input length, breadth and height. Create a user defined function to calculate surface area of four walls and write sub program to calculate surface area of box [Hint: surface area of for walls-2H(L+B), surface area of box=Ih+bh+lb
14.Write a program in QBASIC that allows user to enter radius of a circle. Create a user define function to find the area of circle and sub procedure to find volume of a cylinder. Hint: [A= r2 v=xr2hj
15.Write a program to define a function procedure to display area of sphere and sub procedure to display volume of sphere where user has to input the required data in the main module. [Hint: Area of sphere: 4*PI*R^2, Volume-V=4/3
16.Write a program in QBASIC that asks two numbers to find square of two numbers using SUB procedure and sum of two numbers using FUNCTION procedure.
- 1. Modular Programming:Modular programming is a coding approach where a program is divided into small, independent, and manageable modules or units. Each module performs a specific task and can be developed and tested independently, promoting code reusability and maintainability.
- 2. Procedure:A procedure is a set of instructions or a block of code designed to perform a specific task. Types of procedures include:
- Built-in Procedures: Predefined functions provided by the programming language.
- User-Defined Procedures: Created by the programmer to perform custom tasks.
3.Sub-Procedure vs. Function-Procedure:
- Sub-Procedure: Also known as a subroutine or void function, it performs a task but doesn't return a value.
- Function-Procedure: Returns a value after performing a specific task.
- 4.Main-Module:The main module is the central part of a program. It contains the main function where the execution of the program begins. It often calls other modules to perform specific tasks.
5. Local vs. Global Variable:
- Local Variable: Declared and used within a specific function or block, not accessible outside that scope.
- Global Variable: Declared outside any function, accessible throughout the program.
- 6.Call by ReferenceFUNCTION AddNumbers(ByRef a, ByRef b)a = a + 5b = b + 5END FUNCTION' Call by Value FUNCTION SubtractNumbers(ByVal x, ByVal y) x = x - 3 y = y - 3 END FUNCTION ' Main Program DIM num1, num2 num1 = 10 num2 = 7 ' Call by Reference CALL AddNumbers(num1, num2) ' num1 and num2 will be modified ' Call by Value CALL SubtractNumbers(num1, num2)' num1 and num2 will remain unchanged