Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · C++ · 04 Jul 2026 · Hindi + English

Function Overloading in C++ with Examples and Rules

Function overloading lets you write many functions with the same name but different parameters — the compiler picks the right one. Examples, rules and the return-type trap.

The receptionist analogy

Think of a school receptionist named "enquiry". A parent asks about fees — she answers about fees. A student asks about the timetable — she answers about the timetable. Same name, different response depending on what you bring to her. Function overloading is exactly this: several functions share one name, and the compiler picks the right one by looking at the arguments you pass.

Basic example — one name, three jobs

#include <iostream>
using namespace std;

int add(int a, int b)            { return a + b; }
int add(int a, int b, int c)     { return a + b + c; }     // more params
double add(double a, double b)   { return a + b; }          // diff types

int main() {
    cout << add(2, 3)        << endl;   // matches (int, int)
    cout << add(2, 3, 4)     << endl;   // matches (int, int, int)
    cout << add(2.5, 3.5)    << endl;   // matches (double, double)
    return 0;
}
5 9 6.0

The decision happens at compile time: the compiler reads the arguments, finds the matching version, and hard-wires the call. That is why overloading is called compile-time (static) polymorphism.

What makes two functions "different enough"?

ChangeValid overload?Example
Number of parameters✅ Yesadd(a,b) vs add(a,b,c)
Type of parameters✅ Yesadd(int,int) vs add(double,double)
Order of types✅ Yesf(int,char) vs f(char,int)
Only return type❌ NO — compile errorint f(int) vs double f(int)
Only parameter names❌ NO — same functionf(int a) vs f(int b)
The return-type trap: why can't return type alone distinguish? Because the compiler decides by the call — and a call like f(5); (result ignored) gives no clue which return type was wanted. Ambiguity = forbidden. This "why" is the follow-up interviewers love.

The ambiguity error you will definitely meet

void show(int x)    { cout << "int\n"; }
void show(float x)  { cout << "float\n"; }

show(5);       // OK: exactly int
show(5.5);     // ERROR on many compilers: 5.5 is a DOUBLE literal -
               // converting to int or to float are equally good matches!
show(5.5f);    // OK: the f suffix makes it exactly float

Fix: use exact-type literals (5.5f) or add a double overload. Understanding this error message once saves hours later.

Real-world use: constructors and print functions

class Student {
public:
    Student()                       { }              // no info yet
    Student(string n)               { }              // name only
    Student(string n, int marks)    { }              // full info
};   // constructor overloading: create objects in whichever way suits

void print(int n)      { cout << "Number: "  << n; }
void print(string s)   { cout << "Text: "    << s; }
void print(bool b)     { cout << (b ? "Yes" : "No"); }

This is why cout itself can print an int, a string, or a double with the same << — the entire iostream library is built on overloading.

Receptionist वाली analogy

School की receptionist को सोचिए जिसका नाम है "enquiry". Parent fees पूछता है — वह fees बताती है. Student timetable पूछता है — वह timetable बताती है. Same नाम, अलग जवाब — इस पर depend कि आप क्या लेकर आए. Function overloading बिल्कुल यही है: कई functions एक नाम share करते हैं, और compiler आपके pass किए arguments देखकर सही वाला चुनता है.

Basic example — एक नाम, तीन काम

#include <iostream>
using namespace std;

int add(int a, int b)            { return a + b; }
int add(int a, int b, int c)     { return a + b + c; }     // ज़्यादा params
double add(double a, double b)   { return a + b; }          // अलग types

int main() {
    cout << add(2, 3)        << endl;   // (int, int) से match
    cout << add(2, 3, 4)     << endl;   // (int, int, int) से match
    cout << add(2.5, 3.5)    << endl;   // (double, double) से match
    return 0;
}
5 9 6.0

Decision compile time पर होता है: compiler arguments पढ़ता है, matching version ढूंढता है, और call fix कर देता है. इसीलिए overloading को compile-time (static) polymorphism कहते हैं.

दो functions "काफी अलग" कब माने जाते हैं?

बदलावValid overload?Example
Parameters की संख्या✅ हांadd(a,b) vs add(a,b,c)
Parameters के types✅ हांadd(int,int) vs add(double,double)
Types का order✅ हांf(int,char) vs f(char,int)
सिर्फ return type❌ नहीं — compile errorint f(int) vs double f(int)
सिर्फ parameter names❌ नहीं — same functionf(int a) vs f(int b)
Return-type trap: सिर्फ return type से अलग क्यों नहीं हो सकते? क्योंकि compiler call से decide करता है — और f(5); जैसी call (result ignore) से कोई clue नहीं मिलता कि कौन-सा return type चाहिए था. Ambiguity = मना. यही "क्यों" वाला follow-up interviewers को पसंद है.

वह ambiguity error जो आपको ज़रूर मिलेगा

void show(int x)    { cout << "int\n"; }
void show(float x)  { cout << "float\n"; }

show(5);       // OK: exactly int
show(5.5);     // कई compilers पर ERROR: 5.5 DOUBLE literal है -
               // int में बदलना या float में - दोनों बराबर अच्छे matches!
show(5.5f);    // OK: f suffix इसे exactly float बना देता है

Fix: exact-type literals use करें (5.5f) या double overload जोड़ें. यह error एक बार समझ ली तो बाद में घंटों बचेंगे.

Real-world use: constructors और print functions

class Student {
public:
    Student()                       { }              // अभी कोई info नहीं
    Student(string n)               { }              // सिर्फ name
    Student(string n, int marks)    { }              // पूरी info
};   // constructor overloading: जिस तरह सुविधाजनक हो object बनाओ

void print(int n)      { cout << "Number: "  << n; }
void print(string s)   { cout << "Text: "    << s; }
void print(bool b)     { cout << (b ? "Yes" : "No"); }

इसीलिए cout खुद int, string या double को same << से print कर पाता है — पूरी iostream library overloading पर ही बनी है.

Frequently Asked Questions

What is function overloading in C++?

Function overloading lets multiple functions share the same name with different parameter lists; the compiler selects the correct version at compile time based on the arguments — this is compile-time polymorphism.

Can functions be overloaded by return type only?

No, it is a compile error — the compiler resolves calls from arguments alone, and a call that ignores the result gives no clue about which return type was intended.

What is the difference between function overloading and overriding?

Overloading is same name with different parameters in the same scope, resolved at compile time; overriding is a derived class redefining a virtual function with the same signature, resolved at runtime.