In C++, default arguments are values that are provided for function parameters in case the caller doesn't pass an argument for that parameter. Default arguments make functions more flexible by allowing the caller to omit some arguments.
You can specify default values in the function declaration or definition, but not both.
1️⃣ Syntax for Default Arguments
Function Declaration with Default Arguments:
return_type function_name(parameter1 = default_value1, parameter2 = default_value2, ...);
default_value1, default_value2, ... are the values used if the caller doesn’t provide values for these parameters.
Example:
#include <iostream>
using namespace std;
// Function with default arguments
void greet(string name = "Guest", int age = 25) {
cout << "Hello, " << name << ". You are " << age << " years old." << endl;
}
int main() {
greet(); // Uses default values: "Guest" and 25
greet("Alice", 30); // Uses custom values: "Alice" and 30
greet("Bob"); // Uses custom name ("Bob") and default age (25)
return 0;
}
Explanation:
The greet() function has two parameters, name and age, both with default values.
If no arguments are passed, it uses the default values ("Guest" and 25).
If only one argument is passed, the other one uses its default value.
Output:
Hello, Guest. You are 25 years old.
Hello, Alice. You are 30 years old.
Hello, Bob. You are 25 years old.
2️⃣ Default Arguments in Function Definition
You can also specify default values in the function definition. However, if you have already specified default values in the function declaration, you should not specify them again in the definition.
Example: Default Arguments in Definition
#include <iostream>
using namespace std;
// Function declaration with default arguments
void greet(string name = "Guest", int age = 25);
int main() {
greet(); // Uses default values: "Guest" and 25
greet("Alice", 30); // Uses custom values: "Alice" and 30
greet("Bob"); // Uses custom name ("Bob") and default age (25)
return 0;
}
// Function definition with default arguments
void greet(string name, int age) {
cout << "Hello, " << name << ". You are " << age << " years old." << endl;
}
Explanation:
Default values are specified in the declaration of the function, and then the function definition only includes the parameter types without default values.
This ensures that the caller can omit arguments, but the function will still work as expected.
Output:
Hello, Guest. You are 25 years old.
Hello, Alice. You are 30 years old.
Hello, Bob. You are 25 years old.
3️⃣ Rules for Default Arguments:
Default values must be provided from right to left: If you provide a default value for the first parameter, you must provide default values for all subsequent parameters.
Correct:
void greet(string name = "Guest", int age = 25);
Incorrect:
void greet(int age = 25, string name); // Error
Default values can only be specified once: You can specify default values in the function declaration or the definition, but not both.
Correct:
void greet(string name = "Guest", int age = 25); // In declarationvoid greet(string name, int age) { ... } // In definition
Incorrect:
void greet(string name = "Guest", int age = 25); // In declarationvoid greet(string name, int age = 25) { ... } // In definition (error)
Function Overloading and Default Arguments: Be careful when using default arguments in overloaded functions, as it can sometimes cause ambiguity.
4️⃣ More Examples
Example 1: Default Arguments with Multiple Parameters
#include <iostream>
using namespace std;
void display(int x = 10, int y = 20, int z = 30) {
cout << "x = " << x << ", y = " << y << ", z = " << z << endl;
}
int main() {
display(); // Uses all default values
display(100); // Custom x, default y and z
display(100, 200); // Custom x and y, default z
display(100, 200, 300); // Custom x, y, and z
return 0;
}
Output:
x = 10, y = 20, z = 30
x = 100, y = 20, z = 30
x = 100, y = 200, z = 30
x = 100, y = 200, z = 300
Example 2: Using Default Arguments in Overloaded Functions
#include <iostream>
using namespace std;
void display(string text = "Hello") {
cout << text << endl;
}
void display(string text, int times) {
for (int i = 0; i < times; i++) {
cout << text << " ";
}
cout << endl;
}
int main() {
display(); // Uses default argument: "Hello"
display("Hello", 3); // Custom text and times
return 0;
}
Output:
Hello
Hello Hello Hello
Key Points About Default Arguments:
Optional Parameters: Default arguments allow you to make parameters optional, providing a default value when the argument is omitted.
Right to Left Assignment: When using default arguments, you must specify default values from right to left (i.e., you cannot skip parameters in the middle).
Ambiguity: Default arguments can cause ambiguity in function overloading, so use them carefully to avoid conflicts.
Single Declaration or Definition: Default arguments should only be provided in the function declaration or definition, but not both.
Would you like to explore other topics, such as function pointers or recursion in C++?
0 Comments