← Back to C++ Course | Chapter 5: Functions | Lesson 7 of 13

C++ Function Overloading

Function overloading means giving several functions the same name but different inputs, like the word draw working for a circle or a square. C++ picks the right one by what you give it.
Syntax
cpp
return_type function_name(type1 param);
return_type function_name(type2 param);
return_type function_name(type1 param1, type2 param2);

Overloading by Parameter Type

C++ allows several functions to share the exact same name as long as their parameter types differ, so void print(int x) and void print(double x) can coexist — the compiler picks the right one automatically based on the argument type you pass at the call site.

This is called function overloading.

Example: Overloading by Parameter Type

cpp
// Include std::cout and std::cin
#include <iostream>

void print(int x) { std::cout << "int: " << x << std::endl; }
void print(double x) { std::cout << "double: " << x << std::endl; }

// Program execution starts in main()
int main() {
	print(5);
	print(5.5);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Overloading by Number of Parameters

Functions can also be overloaded by how many parameters they take, so void log(string msg) and void log(string msg, string level) can both exist under the same name — the compiler distinguishes them purely by counting the arguments supplied at each call.

Example: Overloading by Number of Parameters

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

void log(std::string msg) { std::cout << msg << std::endl; }
void log(std::string msg, std::string level) { std::cout << "[" << level << "] " << msg << std::endl; }

// Program execution starts in main()
int main() {
	log("Started");
	log("Started", "INFO");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Overloading by Order of Parameters

Even when two functions share the same set of parameter types, reordering those types — like void combine(int a, double b) versus void combine(double a, int b) — creates two genuinely distinct, valid overloads, since the compiler still sees them as different parameter signatures.

Example: Overloading by Order of Parameters

cpp
// Include std::cout and std::cin
#include <iostream>

void combine(int a, double b) { std::cout << "int,double: " << a << "," << b << std::endl; }
void combine(double a, int b) { std::cout << "double,int: " << a << "," << b << std::endl; }

// Program execution starts in main()
int main() {
	combine(1, 2.5);
	combine(2.5, 1);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Best Practices in Overloading

Overloaded functions with the same name should perform genuinely related, conceptually similar tasks — for example, several area() overloads for different shapes — rather than being unrelated operations that just happen to share a convenient name.

Reusing a name for unrelated behavior makes code harder to understand at a glance.

Example: Best Practices in Overloading

cpp
// Include std::cout and std::cin
#include <iostream>

double area(double side) { return side * side; }
double area(double length, double width) { return length * width; }

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << area(4.0) << std::endl;
	// Print to the console with cout
	std::cout << area(3.0, 5.0) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Resolving Overload Ambiguity

The compiler occasionally can't determine which overload you meant, usually when an argument's type is ambiguous between two candidates — passing a literal 0 where both an int and a pointer overload exist is a classic example.

When this happens, you'll get a compile-time 'ambiguous call' error rather than a silent wrong choice, which at least surfaces the problem immediately.

Example: Resolving Overload Ambiguity

cpp
#include <iostream>

void show(int x) { std::cout << "int: " << x << std::endl; }
void show(double x) { std::cout << "double: " << x << std::endl; }

int main() {
	show(5);      // clearly int
	show(5.0);    // clearly double
	// show('a'); // ambiguous-prone: char can convert to either
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Overloading only by return type, which is not allowed.
  2. Calling an overloaded function with an argument that matches two versions equally, causing an ambiguity error.
  3. Expecting int and const int parameters to be different overloads, when top-level const is ignored.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.