Lesson 11 +30 XP

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.

C++
#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:

OUTPUT
Square    area(5)     = 25
Rectangle area(4, 6)  = 24
Circle    area(2.0)   = 12.5664

What does NOT count as overloading

Pair of declarationsOverloaded?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);❌ errorthe return type alone does not distinguish functions
void f(int a); and void f(int b);❌ redeclarationparameter names do not matter
void f(int); and void f(const int);❌ redeclarationtop-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.

  1. Exact match: the argument types match exactly (trivial conversions such as array to pointer or adding const also count).
  2. Promotion: integral promotions (char, short and bool to int) and float to double.
  3. Standard conversions: for example int to double, double to int, long to int, or a derived-class pointer to a base-class pointer.
  4. User-defined conversions: through constructors or conversion operators of a class (Lesson 21).
  5. 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.

C++
#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:

OUTPUT
show(int): 10
show(double): 3.5
show(char): A
show(double): 2.5
show(int): 7
SolvedExample 1 · Which function does each call choose?

Given void f(int); and void f(double);:

CallArgument typeStepChosen
f(5)intexact matchf(int)
f(5.0)doubleexact matchf(double)
f('x')charpromotion to intf(int)
f(5.0f)floatpromotion to doublef(double)
f(5L)longconversion to int or double: a tieambiguous
Answeronly f(5L) fails to compile.

3. A Complete Program: Overloaded volume()

This is the version found in most textbooks and many past papers.

C++
#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:

OUTPUT
Cube of side 10           : 1000
Cylinder r = 2.5, h = 8   : 157.079
Box 100 x 75 x 15         : 112500

4. 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 overloadingDefault arguments
several function bodiesone body
each version can do different workthe work is the same; only values change
arguments may differ in typeonly 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).
💡 Tip

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.
🎯 END-TERM EXAM PREP

📝 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 area program 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 show program, and the f(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)