UMBC CMSC 201
Fall '06

CSEE | 201 | 201 F'06 | lectures | news | help

Modularity

Example: The Cookbook

A good everyday example of modularity can be found in a cookbook. Let's say we want to make Boston Cream style doughnuts. These are the ones with that great vanilla cream filling and the chocolate icing on top. If we look up the recipe for Boston Cream doughnuts, it will give us the ingredients and instructions for baking the cake part of the doughnut. The recipe will go on to say :
The recipe for Very-Vanilla Custard is a module and so is the recipe for Deep-Dark Chocolate Frosting. The cookbook could have given all of the ingredients and instructions for making these things as part of the Boston Cream Doughnut recipe. By making these recipes be independent modules, many other recipes can also use them. For instance, now the cookbook can refer to the recipe for Deep-Dark Chocolate Frosting as the frosting for chocolate cake.

Definition of a Module

The American Heritage Dictionary of the English Language defines a module as follows:

Functions as Modules

So functions can be used as modules. They are the building blocks of programs. The modular approach can also save us time and effort in the longrun. If we strive to write functions that are very general in nature, then we may be able to reuse the function in some other project that requires solving some similar problem.

Here is an example of a very general and reusable function.


/***********************************************************************
* GetValidInt(min,max) reads integers from the user until one between
* min and max (inclusive) is entered, reprompting the user in
* response to bad ones.  The eventual ggood value is returned.
*
* INPUTS: min and max, the minimum and maximum (inclusive) values for
*         the entered integer
* OUTPUT: an integer between min and max (inclusive)
**********************************************************************/

int GetValidInt(int min, int max)
{
    /* is set greater than max so the loop will be entered*/
    int input = max + 1;

    /* Loop assures a valid entry */
    while( input < min || input > max )
    {
        printf("Please enter an integer between");
        printf(" %d and %d : ", min, max);
        scanf("%d", &input);
    }

    return input;
}


CSEE | 201 | 201 F'06 | lectures | news | help

Thursday, 21-Sep-2006 13:18:59 EDT