Function Default Arguments, Placeholder Parameters, and Overloading in C++
Default Parameters
When defining function parameters with default values, keep two rules in mind:
- If a parameter has a default value, all parameters to its right must also have default values.
- Default values can only be specified in either the declaration or the definition, not both.
#include <iostream>
using namespace std;
int calculate(int x = 5, int y = 20, int z = 15) {
return x + y + z;
}
// Declaration with defaults
int compute(int p = 8, int q = 12);
// Definition without defaults for the same parameters
int compute(int p, int q) {
return p * q;
}
int main() {
cout << calculate() << endl; // Output: 40
cout << compute() << endl; // Output: 96
return 0;
}
Placeholder Parameters
Placeholder parameters occupy a position in the parameter list but don't create a usable variable name. They must either receive an argument during the call or have a default value.
#include <iostream>
using namespace std;
void display(int value, int = 100) {
cout << "Value: " << value << endl;
}
int main() {
display(50); // Second argument uses default value 100
display(50, 200); // Second argument is explicitly 200
return 0;
}
Function Overloading
Function overloading allows multiple functions with the same name to coexist, as long as their parameter lists differ. The compiler distinguishes them by the function signature.
Requirements for overloading:
- All functions must be in the same scope
- Function names must be identical
- Paraemter types, count, or order must differ
void process(int num) { } // Different type
void process(double num) { } // Different type
void process(int a, double b) { } // Different count
void process(double a, int b) { } // Different order
// Return type alone does NOT create an overload
int process(double num); // Error: only differs by return type
Important Considerations:
-
Reference parameters affect overload resolution. Creating a non-const reference from a literal is illegal, so the compiler selects the const overload when passing constants.
-
Avoid ambiguity when overloading functions that have default arguments. If one overload has default parameters and another doesn't, calls with fewer arguments become ambiguous.
#include <iostream>
using namespace std;
void output(int& ref) {
cout << "Non-const reference" << endl;
}
void output(const int& ref) {
cout << "Const reference" << endl;
}
void execute(int x, int y = 50) {
cout << "Two parameters with default" << endl;
}
void execute(int x) {
cout << "Single parameter" << endl;
}
int main() {
int val = 42;
output(val); // Calls non-const version
output(100); // Calls const version (int& ref = 100 is illegal)
execute(30); // Ambiguous - compiler cannot decide between two overloads
execute(30, 70); // Calls two-parameter version
return 0;
}
Key takeaways:
int& ref = 10;is illegal because references must be bound to lvalues- Use
const int& ref = 10;when literals need to bind to references - Default parameters combined with overloads create ambiguous calls that the compiler cannot resolve