In the program, the nCount variable is actually declared in the for() statement, where it is first used:
for (int nCount = nStart; nCount < nStart + 16; nCount++)
This sequence makes it easier to construct loops and other blocks without placing the block’s variables in the program where they are obviously not used.
Default Function Argument Values
When writing functions, you may often create a function that requires many of its parameters for some purposes, yet other calls need only the first few parameters.
You also sometimes need functions that seem to have a variable number of arguments, and you don’t want to code a parameter describing the number of arguments.
Finally, some functions often use default values for some parameters. It is then up to the programmer to code these default values for each call of the function. Heaven forbid should one of the defaults change: you’ll be changing each of the call by hand— a long and tedious process.
C++ provides a solution: specify default values for parameters. This process is simple, being done in the function’s prototype. Listing C.5 is the EXAMP4.CPP program, which demonstrates how to implement default arguments to a function.
Listing C.5. EXAMP4.CPP—Program showing default values for arguments.
//Program EXAMP4.CPP, written 27 July 1992 by Peter D. Hipson
//Shows the use of default values for functions arguments.
#include <limits.h> #include <float.h> #include <iostream.h>
//Defined are a double, long, and an int max() function. You can
//also have others, such as char, float, and so on.
//
//In this version, you have four parameters and find the max of
//the four. Because the minimum number of arguments is two, the final