Modular Programming

Tom Kelliher, CS17

Mar. 18, 1996

``Real world'' programming:

How is the ``mess'' managed?

Break a program into pieces: files, modules, functions.

Examples:

Divide and conquer.

Modular programming:

Organizing a program into small, independent modules that are separately named and individually addressable program elements. These modules are integrated to become a software system that satisfies the problem requirements.

Advantages:

  1. Code reuse.
  2. Code portability.
  3. Permits teamwork.
  4. Easier testing, debugging, maintenance, etc.
  5. Easier to understand --- abstraction.

An Example

Consider the problem of determining the number of digits in each number of a set of numbers.

Pieces of the problem:

  1. Printing instructions.
  2. Asking if user wants to continue.
  3. Getting input.
  4. Calculating number of digits.
  5. Printing answer.

A program:

/**********************************************************************
 * modules.cc
 * Tom Kelliher
 *
 * This is a small program which demonstrates the use of functions
 * (modules) in C++.  The program queries the user for positive input
 * integers and outputs the number of digits in each integer.  At each
 * step, the user is asked if they wish to continue
 **********************************************************************/


#include <iostream.h>


// function declarations (prototypes)

void printInstructions(void);
int keepGoing(void);
long inputNumber(void);
int numberOfDigits(long n);
void outputNumberOfDigits(int n, int d);


/**********************************************************************
 * main
 **********************************************************************/

int main()
{
   long number;   // An input number.
   int digits;    // The number of digits in number.

   printInstructions();

   while (keepGoing())
   {
      number = inputNumber();
      digits = numberOfDigits(number);
      outputNumberOfDigits(number, digits);
   }

   return 0;
}


/**********************************************************************
 * printInstructions --- Print a list of instructions for the user.
 **********************************************************************/

void printInstructions(void)
{
   cout << "This program takes numbers as inputs and outputs the\n";
   cout << "number of digits in each of the numbers.  It will ask you\n";
   cout << "each time if you wish to continue.  Enter \"y\" to\n";
   cout << "continue and anything else to exit.\n\n";
}


/**********************************************************************
 * keepGoing --- Ask the user if they want to keep going.  Return 1
 * if they do, otherwise return 0.
 **********************************************************************/

int keepGoing(void)
{
   char c;   // Input character.

   cout << "Do you wish to continue (\"y\" or \"n\")? ";
   cin >> c;

   if (c == 'y')
      return 1;
   else
      return 0;
}


/**********************************************************************
 * inputNumber --- Input a positive integer from the user.
 **********************************************************************/

long inputNumber(void)
{
   long n = 0;   // The input integer.

   while (n <= 0)
   {
      cout << "Enter a positive integer: ";
      cin >> n;
   }

   return n;
}


/**********************************************************************
 * numberOfDigits --- Determine and return the number of digits in n,
 * the function parameter.
 **********************************************************************/

int numberOfDigits(long n)
{
   int digits = 0;   // Number of digits encountered so far.

   while (n != 0)
   {
      n /= 10;    // Remove one digit.
      ++digits;   // Increment digit count.
   }

   return digits;
}


/**********************************************************************
 * outputNumberOfDigits --- Output the original number (the parameter
 * n)and the number of digits (d) in n.
 **********************************************************************/

void outputNumberOfDigits(int n, int d)
{
   cout << n << " has " << d << " digit(s).\n";
}

Function Definitions

The actual definition of how the function does its work.

Contains:

  1. A function return type (e.g., void, int).
  2. A function name (naming rules?).
  3. A list of formal parameters or void.
  4. A compound block.

int numberOfDigits(long n)
{
   int digits = 0;   // Number of digits encountered so far.

   while (n != 0)
   {
      n /= 10;    // Remove one digit.
      ++digits;   // Increment digit count.
   }

   return digits;
}

Function Calls

Function name followed by list of actual parameters.

digits = numberOfDigits(number);

Function Declarations

The declaration of the function's ``look and feel:''

int numberOfDigits(long n);

Global, local prototypes:

Function prototype scope:

The part of the program in which the declared function is recognized.


Thomas P. Kelliher
Fri Mar 15 18:14:23 EST 1996
Tom Kelliher