C++ course menu
C++ Function Overloading and Default Arguments
Learn C++ function overloading (several functions with the same name and different parameters), how the compiler picks the right overload, ambiguous calls, default arguments, and when to choose each.
On this page
In C++, two functions can share a name as long as their parameters differ. This is overloading, and it lets you give the same operation one natural name for different types. Default arguments let callers leave out parameters that usually have the same value.
Function overloading#
#include <iostream>
#include <string>
void print(int value) {
std::cout << "int: " << value << '\n';
}
void print(double value) {
std::cout << "double: " << value << '\n';
}
void print(const std::string& value) {
std::cout << "string: " << value << '\n';
}
int main() {
print(42);
print(3.5);
print(std::string("hello"));
}int: 42
double: 3.5
string: helloThe compiler chooses which print to call by looking at the argument types. This happens while compiling, so there is no run-time cost. std::cout << x itself works this way: there is a separate << for each type.
Overloads can differ in the number of parameters too:
#include <iostream>
int area(int side) {
return side * side;
}
int area(int width, int height) {
return width * height;
}
int main() {
std::cout << area(5) << ' ' << area(4, 6) << '\n';
}25 24What does not count as different#
Overloads must differ in their parameter types or count. A different return type alone is not enough:
int parse(const char* text);
double parse(const char* text); // error: differs only in return typeThe compiler could not tell which one parse("42") means.
Ambiguous calls#
Sometimes no overload is a clear best match, and the call is rejected:
#include <iostream>
void show(int x) { std::cout << x; }
void show(double x) { std::cout << x; }
int main() {
show(5L); // long: converts equally well to int and double
}The fix is to make the argument's type explicit (show(5) or show(5.0)), or to add an overload for that type.
Default arguments#
A default argument is used when the caller leaves that argument out:
#include <iostream>
#include <string>
void greet(const std::string& name, const std::string& greeting = "Hello", int times = 1) {
for (int i = 0; i < times; ++i) {
std::cout << greeting << ", " << name << "!\n";
}
}
int main() {
greet("Asha");
greet("Ravi", "Welcome");
greet("Meera", "Hi", 2);
}Hello, Asha!
Welcome, Ravi!
Hi, Meera!
Hi, Meera!Rules for default arguments:
- They must come last: once a parameter has a default, every parameter after it needs one too.
- Arguments still match by position, so you cannot skip
greetingand give onlytimes. - Write the default once, in the declaration (usually in the header), not again in the definition.
Overloading or default arguments?#
Use default arguments when the function does the same thing and one parameter usually has a typical value (greeting = "Hello").
Use overloading when the types differ or the work is genuinely different for each version (printing an int vs a std::string).
When many overloads differ only by type but contain the same code, a template is often better; see Templates.
Common mistakes#
Practice#
Practice exercise
Write three overloads of max_of: one for two ints, one for three ints (reuse the first), and one for two doubles. Print max_of(3, 9), max_of(4, 12, 7) and max_of(2.5, 1.5).
Show solution
#include <iostream>
int max_of(int a, int b) {
return a > b ? a : b;
}
int max_of(int a, int b, int c) {
return max_of(max_of(a, b), c);
}
double max_of(double a, double b) {
return a > b ? a : b;
}
int main() {
std::cout << max_of(3, 9) << ' ' << max_of(4, 12, 7) << ' ' << max_of(2.5, 1.5) << '\n';
}9 12 2.5In real code, std::max from <algorithm> already does this for any type.
Practice exercise
Write double price_with_tax(double price, double rate = 0.18) and print the result for 1000 with the default rate and with a rate of 0.05.
Show solution
#include <iostream>
double price_with_tax(double price, double rate = 0.18) {
return price * (1 + rate);
}
int main() {
std::cout << price_with_tax(1000) << '\n';
std::cout << price_with_tax(1000, 0.05) << '\n';
}1180
1050Key takeaway#
Finished this lesson?