Modular Programming in QBasic
What is Modular Programming?
Modular programming is a method where large programs are broken down into smaller, manageable pieces
called modules. Each module handles a specific part of the task, making the program easier to understand
and maintain.
Benefits of Modular Programming
Modular programming has several benefits:
1. **Easier to Maintain**: Small modules are easier to debug and maintain.
2. **Reusability**: Modules can be reused in other programs.
3. **Easier Collaboration**: Different modules can be developed by different programmers simultaneously.
4. **Simplifies Complex Programs**: Breaking down complex programs into smaller modules makes them
easier to manage.
Modular Programming in QBasic
QBasic uses functions and sub-procedures to implement modular programming. Functions return a value
after completing a task, while sub-procedures simply perform tasks without returning a value.
Sub Procedures
Sub-procedures are used when a task needs to be done without returning any value. They are defined using
the `SUB` keyword and can be called as needed within the program.
Functions
Functions perform a task and return a result. They are useful when you need a calculation or data to be
returned after completing the function's task. Functions are defined using the `FUNCTION` keyword.
Example 1: Finding the Area of a Circle
```qbasic
DECLARE FUNCTION AreaOfCircle (radius AS SINGLE)
INPUT "Enter the radius of the circle: ", radius
PRINT "The area of the circle is: ", AreaOfCircle(radius)
FUNCTION AreaOfCircle (radius AS SINGLE)
CONST pi = 3.14159
AreaOfCircle = pi * radius * radius
END FUNCTION
```
Example 2: Finding the Volume of a Cube
```qbasic
DECLARE FUNCTION VolumeOfCube (side AS SINGLE)
INPUT "Enter the side length of the cube: ", side
PRINT "The volume of the cube is: ", VolumeOfCube(side)
FUNCTION VolumeOfCube (side AS SINGLE)
VolumeOfCube = side ^ 3
END FUNCTION
```
Example 3: Finding the Surface Area and Volume of a Hemisphere
```qbasic
DECLARE FUNCTION SurfaceAreaHemisphere (radius AS SINGLE)
DECLARE FUNCTION VolumeHemisphere (radius AS SINGLE)
INPUT "Enter the radius of the hemisphere: ", radius
PRINT "Surface area of the hemisphere: ", SurfaceAreaHemisphere(radius)
PRINT "Volume of the hemisphere: ", VolumeHemisphere(radius)
FUNCTION SurfaceAreaHemisphere (radius AS SINGLE)
CONST pi = 3.14159
SurfaceAreaHemisphere = 3 * pi * radius * radius
END FUNCTION
FUNCTION VolumeHemisphere (radius AS SINGLE)
CONST pi = 3.14159
VolumeHemisphere = (2 / 3) * pi * radius * radius * radius
END FUNCTION
```