Skip to main content

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

  1. Module – a self-contained unit (function, procedure, file, package or class) with a name, inputs, outputs and a single purpose.
  2. Decomposition – the problem is split into sub-problems, usually top-down, until each piece is small enough to code easily.
  3. Interface – each module exposes only what others need (function names, parameters, return values); callers depend on the interface, not the code.
  4. Information hiding – internal data and algorithms are hidden inside the module (Parnas), so they can change without affecting other modules.
  5. High cohesion – all statements in a module work toward one task.
  6. Low coupling – modules depend on each other as little as possible, ideally passing only the data they need.
  7. Independent development and compilation – modules can be written, compiled and unit-tested separately, by different programmers.
  8. 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

All Software Engineering chapter 3 questions →