A car is not built as one solid piece. It is assembled from an engine, a gearbox, wheels and many other units, each made and tested separately. Large programs are built in the same way, from separate parts called modules.
Modules
Definition: Module. A self-contained part of a program that performs one clearly defined task and communicates with the rest of the program through a defined interface.
In C a module is normally written as a function. The interface of a module states what it receives and what it gives back. Other parts of the program need to know only the interface, not how the task is carried out inside.
Definition: Modular programming. A method of designing a program as a collection of separate modules, each responsible for one task, which are combined to solve the whole problem.
Benefits of Modular Design
- Easier to understand: each module is small and has a single purpose.
- Easier to test: a module can be tested by itself before it is combined with others.
- Easier to correct and change: a fault lies within one module, and changing a module does not disturb the others provided its interface stays the same.
- Reusable: a module such as "find the largest value" can serve many programs.
- Shared work: several programmers can write different modules at the same time.
Top-Down Design
Definition: Top-down design. An approach that begins with the whole problem and divides it step by step into smaller subproblems until each is simple enough to be coded directly.
This is the decomposition studied in Unit I, carried through to program design. The result is a structure chart, in which the main module is at the top and the detailed modules are at the bottom. Top-down design is also called stepwise refinement.
Bottom-Up Design
Definition: Bottom-up design. An approach that begins by building and testing small, general-purpose modules and then combines them into larger modules until the whole problem is solved.
Bottom-up design suits situations in which useful low-level modules already exist or are plainly needed, such as a library of mathematical functions. Table 4.1 compares the two approaches.
Table 4.1: Comparison of top-down and bottom-up design
| Point | Top-down | Bottom-up |
|---|---|---|
| Starting point | The whole problem | Small basic modules |
| Direction | Divide into smaller parts | Combine into larger parts |
| Main strength | The overall structure is clear early | Tested, reusable parts are available early |
| Main risk | Low-level difficulties appear late | Parts may not fit the final need |
In practice the two are used together: the overall design proceeds top-down, while commonly needed modules are built and tested bottom-up.
Qualities of a Good Module
A good module does one job, is small enough to be read at a glance, has a name that states its purpose, and depends on other modules only through their interfaces.
Example 4.1: A program built from modules
Problem: Using top-down design, write a modular program that finds the total and average of three marks.
Solution:
- Write one function for the total and one for the average.
- The function for the average reuses the function for the total.
- The main function reads the data, calls the modules and displays the report.
#include <stdio.h>
int findTotal(int a, int b, int c);
float findAverage(int a, int b, int c);
int main(void)
{
int m1, m2, m3;
printf("Enter three marks: ");
scanf("%d %d %d", &m1, &m2, &m3);
printf("\nTotal = %d\n", findTotal(m1, m2, m3));
printf("Average = %.2f\n", findAverage(m1, m2, m3));
return 0;
}
int findTotal(int a, int b, int c)
{
return a + b + c;
}
float findAverage(int a, int b, int c)
{
return findTotal(a, b, c) / 3.0f;
}
Input:
70 85 91
Output:
Enter three marks:
Total = 246
Average = 82.00
Result: Each module has one task. findAverage reuses findTotal, so the rule for the total is written in only one place.
Common mistakes
- Writing one very long main function instead of dividing the work into modules.
- Giving a module several unrelated tasks, such as calculating and displaying in the same function.
- Making a module depend on the internal details of another instead of on its interface.
Key takeaways
- A module performs one defined task and has a clear interface.
- Top-down design divides the whole problem into smaller parts.
- Bottom-up design combines tested small modules into larger ones.
- Modular programs are easier to understand, test, change and reuse.
Practice questions
- What is a module?
- Which design approach starts with the whole problem?
- State two benefits of modular programming.
Answers to the practice questions
- A self-contained part of a program that performs one clearly defined task and has a defined interface.
- Top-down design.
- Any two of: easier understanding, easier testing, easier correction and change, reuse, shared work.
Sources
- Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit IV
