Default Arguments in Function
C++
Medium
5 views
Problem Description
Create function with default parameter values.
Real Life: Like having default settings that can be changed if needed.
Step-by-Step Logic:
1. Give default value to parameters in declaration
2. If argument not passed, default is used
3. If argument passed, it overrides default
4. Provides flexibility in function calls
Official Solution
int multiply(int a, int b = 2) { // b has default value 2
return a * b;
}
void function_q10_default_args() {
cout << "10 * 2 (default): " << multiply(10) << endl;
cout << "10 * 5 (custom): " << multiply(10, 5) << endl;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!