- Year not given · 5 marks
Basic concepts of modular programming
Short answer
Modular programming splits a program into named modules with clear interfaces, information hiding, high cohesion and low coupling, enabling independent development, testing and reuse. Example: a student result program has main() calling readMarks(), calcPercent(), getGrade() and printReport(); changing grading rules requires changing only getGrade().
What are basic concept of modular programming? Explain with example.
Modular programming is a design and programming technique in which a large program is divided into small, separate, named units called modules, each performing one well-defined task and interacting with other modules only through clear interfaces.
Basic concepts
- Module – a self-contained unit (function, procedure, file, package or class) with a name, inputs, outputs and a single purpose.
- Decomposition – the problem is split into sub-problems, usually top-down, until each piece is small enough to code easily.
- Interface – each module exposes only what others need (function names, parameters, return values); callers depend on the interface, not the code.
- Information hiding – internal data and algorithms are hidden inside the module (Parnas), so they can change without affecting other modules.
- High cohesion – all statements in a module work toward one task.
- Low coupling – modules depend on each other as little as possible, ideally passing only the data they need.
- Independent development and compilation – modules can be written, compiled and unit-tested separately, by different programmers.
- Reusability – a general module (e.g. date validation) can be reused in many programs.
Example: student result processing in C
+-------------+
| main() |
+-------------+
+-----------+-----+-------+------------+
| | | |
+-------------+ +---------------+ +-----------+ +---------------+
| readMarks() | | calcPercent() | | getGrade()| | printReport() |
+-------------+ +---------------+ +-----------+ +---------------+
void readMarks(int m[], int n); /* input module */
float calcPercent(int m[], int n); /* compute module */
char getGrade(float pct); /* grading rule */
void printReport(float pct, char g); /* output module */
int main(void) {
int marks[5];
readMarks(marks, 5);
float p = calcPercent(marks, 5);
printReport(p, getGrade(p));
return 0;
}
If the university changes its grading rules, only getGrade() is changed; the other modules are untouched.
Advantages
- Easier to understand, code and debug.
- Parallel development by a team.
- Easier testing (unit testing per module) and maintenance.
- Reuse of modules in other programs.
✓ Answer checked by Claude Sonnet 5.5
Related questions
- Basic requirements of linear programming · Project Engineering, chapter 3
- Cloud and grid computing concepts · Internet and Intranet, chapter 7 · asked 2×
- Simple program illustrating OOP concept · Object Oriented Programming, chapter 1
- Concept of class with example · Object Oriented Programming, chapter 1