Default Parameters
#include <iostream> using namespace std; // Print a row with a starting and ending character, with a specified number // of middle characters between them. void row(char ends, int num_middle, char middle) { cout << ends; for(int m = num_middle; m--;) cout << middle; cout << ends << endl; } // Draw a box with text. void box(int width = 9, int height = 5, bool fill = false) { // Top row row('+', width-2, '-'); // Draw internal rows. for(int m = height-2; m--;) row('|', width-2, fill ? '#' : ' '); // Final row. row('+', width-2, '-'); } // Box driver. int main() { box(); cout << endl; box(4); cout << endl; box(15, 7); cout << endl; box(3,3,true); }

C++ allows parameters to be given default values which are used when no argument is provided for that position. Defaults can be convenient, but do have some limits:

So you can't make calls like f(5,6,,4);, where you only leave out arguments in the middle, and you have to provide defaults so it is possible to leave out the 4.

A particularly annoying rule forbids you from specifying the default values in both the prototype and function definition.