A function parameter can be given a default value which is used when the caller does not pass an argument for it:
void greet(std::string name = "world") {
std::cout << "Hello " << name << "\n";
}
greet(); // Hello world
greet("there"); // Hello thereParameters with default arguments can only appear at the end of a parameter list:
void f(int a, int b = 2, int c = 3); // OK.
void g(int a = 1, int b); // ERROR.Default arguments are always filled from the end of the parameter list, so a caller cannot skip an earlier default argument:
void f(int a, int b = 2, int c = 3);
f(1); // a = 1, b = 2, c = 3
f(1, 5); // a = 1, b = 5, c = 3
f(1, 5, 9); // a = 1, b = 5, c = 9Default values must go in the function declaration and not the definition:
void f(int a, int b = 2); // Declaration.
void f(int a, int b) {} // Definition.Caution
Repeating the default arguments in the definition is a compiler error.
Dan