# Functions, Loose Ends

Tom Kelliher, CS17

Mar. 27, 1996

# Style Considerations

• Carry out stepwise refinement until each module is expressible in one or two sentences.
• C++ functions, which implement modules, as a rule of thumb, should be between 2 and 50 lines long. (A function should generally fit on a single page.)
• A function should not call too many other functions.
• As always, use descriptive names, preferably verbs.
• Don't use global variables for communication.
• Always use function prototypes. It's easiest to use global prototypes.
• Try to design reusable functions .
• Don't reinvent the wheel.

# Modular Program Design Problems

1. Design a modular program which computes factorials of non-negative integers:

The program should accept positive integers from the user, asking them if they want to continue, and output the factorial of the input.

2. Using modules from the previous program, write a modular program which computes combinations. The formula is:

C(n,r) is the number of ways in which r objects can be drawn from a set of n objects, assuming that order doesn't matter. The user interface of this program should be similar to the previous one. I.e., accept input for several computations, ask the user if they want to continue, etc.

3. Design a modular program that plays a number guessing game with the user. Here is a sample run for the program:
Welcome to the game of Guess It!
I will choose a number between 1 and 100.
You will try to guess that number.  If you guess wrong,
I will tell you if you guessed too high or too low.
You have 6 tries to get the number.

OK, I am thinking of a number.  Try to guess it.

Too high!
Too low!
Illegal guess.  Your guess must be between 1 and 100.
Try again.  Your guess? 23
**** CORRECT ****

Want to play again? y
OK, I am thinking of a number.  Try to guess it.

**** CORRECT ****

Want to play again? n
Goodbye, it was fun.  Play again soon.

Thomas P. Kelliher
Tue Mar 26 12:59:56 EST 1996
Tom Kelliher