Here is an example void foo ( int a, int b = 0 )
There 'a' is a normal parameter, 'b' is optional, if no value is passed for 'b', 0 will be used for it
After you declare an optional parameter, all following parameters must be optional too
If you have the prototype and implementation of the function in two separate places, you must specify the default value only in the prototype
eg
1 2 3 4 5 6
void foo ( int a, int b = 0 ); // or void foo ( int , int = 0 )void foo ( int a, int b )
{
//...
}
#include <iostream>
int count_to(int top, int steps = 1)
{
for (int i = 0; i < top; i+=steps)
std::cout << i << std::endl;
return 0;
}
int main()
{
count_to (10);
count_to(100, 10);
}