Ch-7: Modular programming
1. What is Modular Programming?
Modular Programming is a software design approach that involves breaking down a program into smaller, self-contained modules or units, each responsible for a specific task or functionality. This approach enhances code organization, reusability, and maintenance.
2. What is Procedure? Mention its types.
A procedure is a named block of code in programming that performs a specific task.
Types of procedures include:
Sub procedure: A self-contained block of code that performs a specific task, often without returning a value.
Function procedure: Similar to a subprocedure, but it returns a value after performing a task.
3. Write the difference between sub-procedure and function-procedure.
The difference between subprocedure and function procedure:
Sub procedure: Doesn't return a value, used for tasks like printing or modifying data.
Function procedure: Returns a value after performing a task, often used for calculations or data transformation.
4. Define main-module.
A main module is the primary entry point of a modular program. It typically orchestrates the execution of various submodules or functions and manages the program's overall flow.
5. Differentiate between local and global variable.
Local variable: It's declared within a specific subroutine or function and is accessible only within that scope.
Global variable: It's declared at a higher level, such as the main module, and can be accessed from any part of the program, including subroutines and functions.
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