INITIALIZING FUNCTION PARAMETERS
With all the functions you have used up to now, you have had to take care to provide an argument corresponding to each parameter in a function call. It can be quite handy to be able to omit one or more arguments in a function call and have some default values supplied automatically for the arguments that you leave out. You can arrange this by providing initial values for the parameters to a function in its prototype.
Suppose that you write a function to display a message, where the message is passed as the argument. Here is the definition of such a function:
void showit(const char message[])
{
cout << endl
<< message;
return;
}
You can specify a default value for the parameter to this function by specifying the initializing string in the function prototype, as follows:
void showit(const char message[] = "Something is wrong.");
The default value for the message parameter is the string literal shown. If you omit the argument when you call the function, the default value is used.
// Ex6_03.cpp // Omitting function arguments #include <iostream> using std::cout; using std::endl; void showit(const char message[] = "Something is wrong."); int main(void) { const char mymess[] = "The end of ...
Get Ivor Horton's Beginning Visual C++ 2012 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.