Default arguments in C++
Level 3130 keystrokes
msg has the default value "Hello", which is used when the caller leaves it out. Prints "Hello, Alice!" and "Hi, Bob!".
C++ code
// msg = "Hello" is a default argument, used when the second argument is omitted1void greet(string name, string msg = "Hello") { // Prints the greeting, e.g. Hello, Alice!2 cout << msg << ", " << name << "!" << endl;3}4 // Only name is passed, so msg is "Hello"5greet("Alice");// A second argument replaces the default: Hi, Bob!6greet("Bob", "Hi");Output
Hello, Alice! Hi, Bob!
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this C++ code and how often they appear.
"× 12<× 10,× 3;× 3(× 3)× 3!× 1{× 1}× 1=× 1