Loading lessons...
Function Overloading
Function Overloading
Overloading means using the same name for different purposes. Function overloading lets several functions share one name as long as their parameter lists differ. The compiler picks the right one for each call by looking at the arguments. It is the simplest form of compile-time polymorphism and a near-certain exam question.
1. What Function Overloading Is
Functions with the same name but different argument lists (a different number of arguments, different types, or a different order of types) can coexist in the same scope. The function's name plus its parameter types is called its signature.
#include <iostream>
using namespace std;
// three functions named area, with different signatures
int area(int side) { // area of a square
return side * side;
}
int area(int length, int breadth) { // area of a rectangle
return length * breadth;
}
double area(double radius) { // area of a circle
return 3.14159 * radius * radius;
}
int main() {
cout << "Square area(5) = " << area(5) << endl;
cout << "Rectangle area(4, 6) = " << area(4, 6) << endl;
cout << "Circle area(2.0) = " << area(2.0) << endl;
return 0;
}Output:
Square area(5) = 25
Rectangle area(4, 6) = 24
Circle area(2.0) = 12.5664What does NOT count as overloading
| Pair of declarations | Overloaded? | Why |
|---|---|---|
int f(int); and int f(int, int); | ✅ | different number of arguments |
int f(int); and int f(double); | ✅ | different types |
void f(int, char); and void f(char, int); | ✅ | different order of types |
int f(int); and double f(int); | ❌ error | the return type alone does not distinguish functions |
void f(int a); and void f(int b); | ❌ redeclaration | parameter names do not matter |
void f(int); and void f(const int); | ❌ redeclaration | top-level const on a value parameter is ignored |
void f(int*); and void f(const int*); | ✅ | pointer to int vs pointer to const int |
void f(int&); and void f(const int&); | ✅ | different reference types |
2. How the Compiler Chooses (Overload Resolution)
For a call, the compiler compares the arguments with each candidate, trying these steps in order. The first step that finds exactly one function wins.
- Exact match: the argument types match exactly (trivial conversions such as array to pointer or adding
constalso count). - Promotion: integral promotions (
char,shortandbooltoint) andfloattodouble. - Standard conversions: for example
inttodouble,doubletoint,longtoint, or a derived-class pointer to a base-class pointer. - User-defined conversions: through constructors or conversion operators of a class (Lesson 21).
- Ellipsis: a match with a
...parameter.
If two or more functions match equally well at the first successful step, the call is ambiguous and does not compile.
#include <iostream>
using namespace std;
void show(int x) {
cout << "show(int): " << x << endl;
}
void show(double x) {
cout << "show(double): " << x << endl;
}
void show(char x) {
cout << "show(char): " << x << endl;
}
int main() {
show(10); // exact match: int
show(3.5); // exact match: double
show('A'); // exact match: char
show(2.5f); // float: promoted to double
short s = 7;
show(s); // short: promoted to int
// show(10L); // long: int and double are both standard conversions -> ambiguous
return 0;
}Output:
show(int): 10
show(double): 3.5
show(char): A
show(double): 2.5
show(int): 7Given void f(int); and void f(double);:
| Call | Argument type | Step | Chosen |
|---|---|---|---|
f(5) | int | exact match | f(int) |
f(5.0) | double | exact match | f(double) |
f('x') | char | promotion to int | f(int) |
f(5.0f) | float | promotion to double | f(double) |
f(5L) | long | conversion to int or double: a tie | ambiguous |
f(5L) fails to compile.3. A Complete Program: Overloaded volume()
This is the version found in most textbooks and many past papers.
#include <iostream>
using namespace std;
int volume(int s) { // cube
return s * s * s;
}
double volume(double r, int h) { // cylinder
return 3.14159 * r * r * h;
}
long volume(long l, int b, int h) { // rectangular box
return l * b * h;
}
int main() {
cout << "Cube of side 10 : " << volume(10) << endl;
cout << "Cylinder r = 2.5, h = 8 : " << volume(2.5, 8) << endl;
cout << "Box 100 x 75 x 15 : " << volume(100L, 75, 15) << endl;
return 0;
}Output:
Cube of side 10 : 1000
Cylinder r = 2.5, h = 8 : 157.079
Box 100 x 75 x 15 : 1125004. How It Works: Name Mangling
The linker needs a unique name for every function. The C++ compiler therefore encodes the parameter types into the function's internal name, a process called name mangling (or name decoration).
With GCC, int area(int) becomes _Z4areai and int area(int, int) becomes _Z4areaii. C does not mangle names, which is why C has no overloading and why C++ code that calls C libraries uses extern "C".
5. Overloading vs Default Arguments
Both let one name be called in several ways.
| Function overloading | Default arguments |
|---|---|
| several function bodies | one body |
| each version can do different work | the work is the same; only values change |
| arguments may differ in type | only the number of supplied arguments differs |
| choose when the algorithms differ (area of square vs circle) | choose when the algorithm is the same with usual values (printLine(ch = '-')) |
6. Advantages
- Readability: one meaningful name (
area,print,add) for one idea. - Less to remember: no
areaSquare,areaRectangle,areaCircle. - Compile-time polymorphism: the right function is chosen with no run-time cost.
- Basis for constructor overloading (Lesson 16) and for operator overloading (Lesson 19).
Key points to remember
- Overloaded functions share a name but differ in the number, type or order of parameters.
- The return type alone cannot distinguish overloaded functions.
- Resolution order: exact match → promotion → standard conversion → user-defined conversion → ellipsis; a tie is an ambiguity error.
- Implemented by name mangling; it is compile-time (static) polymorphism.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. What is function overloading? Write a program to find the area of a square, rectangle and circle using overloaded functions. (5 Marks)
- Write: the definition, the rules table (including "return type alone is not enough"), and the
areaprogram with output. - Q2. Explain how the compiler resolves a call to an overloaded function. When is a call ambiguous? (5 Marks)
- Write: the five steps, the
showprogram, and thef(5L)example. - Q3. Write a program using overloaded
volume()functions for a cube, a cylinder and a box. (5 Marks) - Q4. Compare function overloading with default arguments. (2.5 Marks)