Default Parameter Values
Sometimes it's useful to set a default value for a parameter. For example, when a parameter value is often repeated.
Default parameter values allow us to call a function without having to specify all parameter values. If a value for a parameter with a default value is not provided, its default value is used. We set the default value of a parameter during function definition using the = operator. For example:
void greet(std::string name = "world") {
std::cout << "Hello, " << name << std::endl;
}This function has one parameter name of type std::string with a default value of "world". When we call the function without a parameter value:
greet(); // Prints: Hello, worldThe function uses the default value and prints Hello, world.
When we call the function with a parameter value:
greet("Charlie"); // Prints: Hello, CharlieThe function uses the provided value and prints Hello, Charlie.
Instructions
Set the default value of the name parameter in the welcome function to "visitor".
Start programming for free
5/7