Tom Kelliher, CS17
Mar. 18, 1996
``Real world'' programming:
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:
Consider the problem of determining the number of digits in each number of a set of numbers.
Pieces of the problem:
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"; }
The actual definition of how the function does its work.
Contains:
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 name followed by list of actual parameters.
digits = numberOfDigits(number);
The declaration of the function's ``look and feel:''
int numberOfDigits(long n);
Global, local prototypes:
int main() { int numberOfDigits(long n); ... }
Function prototype scope:
The part of the program in which the declared function is recognized.