(x-hour lecture)
This is another feature that is only available in C++, rather than just plain old C. Default parameters are used when one or more of the parameters are missing from a function call. To use them, put them in the function's prototype. For example, to create the bozo function with default parameters, we would type the prototype as
void bozo(int a, int b = 2, int c = 5);Check out the demo lg.cpp to see some functions with default parameters in action. There are three rules for use of default parameters:
So, using our bozo()
function, we could make any of the
following calls:
bozo(7, 6, 3); // a = 7, b = 6, c = 3 bozo(7, 6); // a = 7, b = 6, c = 5 (the default value) bozo(7); // a = 7, b = 2, c = 5 (b & c are default)
These however, are illegal calls:
bozo(); // there is no default value for a bozo(7, , 3); // violates rule #2
And now for something completely different. Well, you haven't got a professor writing the code right before your eager eyes, so this may not be nearly as stimulating as it was in class, but here goes. The file we're trying to create is guessingGame.cpp in the 10/8 Class Examples folder on PUBLIC.
The object of the game is to guess a number between 1 and 100, which the computer has selected. Before each guess, the program informs the user of the range of legal guesses. After each guess, the computer tells the user if it was too high or too low, or, in the case of input that is outside the legal range, the computer will re-prompt for input until a legal guess is entered. Depending on your tastes, the computer can get progressively more annoyed with the user as numbers outside the range are picked. The executable on PUBLIC will get nastier and more insulting, until the program gets fed up with the user and exits.
The first step to writing the program is to figure out what things can be called the state of the game. State is all the information we need to know about a game so that if it were interrupted, we could reconstruct the game exactly as it was at some later point. For example, in chess, the state would include whose move it is, the pieces on the board and their positions, whether each player has castled, the previous move (for the en passant rule), and time left for the move. In a baseball game, state would include the score, the inning, the players on the field, any players on base, players who are out of the game, and the count for the current batter. For our simple guessing game, the state consists of the range of legal guesses (expressed as two ints: high and low), the target number, and the total number of tries.
Next, come up with a brief description of what the program should do. try to break it into bite-sized pieces (note the foreshadowing here). For example, the guessing game could be broken down like this:
// repeatedly ask user for number // tell if the number is high or low // update stateNote that the comments are written first, and then the C++ is written around the comments. At the end, your comments should match your code.
The three comment lines, which broke the program into smaller
chunks can now be implemented as functions. (You knew that was
coming, right?) The trick is to know which state variables should be
passed in as parameters to each function. The first function, which
is named GetAGuess
, needs only the range of legal
guesses, and it can then repeatedly ask until the user gives it a
legal value, which is then its return value. That means that we need
to use the prototype
int GetAGuess(int high, int low);You probably already guessed that we need a while-loop, so that we can repeatedly ask for input as long as the number the user enters makes
(guess < low || guess > high)
a true statement.
The next function is
void InformPlayer(int guess, int target)This just compares the two values, and tells the player if the guess was too high, too low, or if it was, in fact, the correct answer.
The last function,
void UpdateInterval(int guess, int target, int& low, int& high);takes parameters by reference, so that it can alter the value of either one it needs to. Note here that the range is adjusted to be one number closer to the target than the guess.
The important thing here isn't really the game, but the way the game was created. Before you write any code down, know the basics of what you want to do. Know the variables that make up the state of the program. Write the basic steps as comments, then fill them with code, breaking up actions into functions whenever you can. Initialize variables to hold the state, and give them logical names. And may the force be with you.
To Index Previous Next