🔵 Core C++  ·  Lesson 30

Function Overloading

What is function overloading?

Function overloading lets you give several functions the same name, as long as their parameter lists differ — a different number of parameters, or different types. C++ figures out which one you meant from the arguments you pass.

Why overload?

Without overloading you would need clumsy names for the same idea: addInts, addDoubles, addThree. Overloading lets one clear name — add — serve them all, so callers do not have to remember variants.

A working example

C++
#include <iostream>
using namespace std;

int add(int a, int b)          { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c)   { return a + b + c; }

int main() {
    cout << add(2, 3) << "\n";        // int version
    cout << add(2.5, 3.5) << "\n";    // double version
    cout << add(1, 2, 3) << "\n";     // three-arg version
    return 0;
}
Output:
5
6
6

How the compiler chooses

The compiler matches your call to the version whose parameters best fit the argument types and count — a process called overload resolution. In the example, add(2, 3) matches the two-int version, add(2.5, 3.5) matches the two-double version, and so on.

What counts as different

DifferenceValid overload?
Different number of parametersYes
Different parameter typesYes
Different order of typesYes (e.g. (int,double) vs (double,int))
Only different return typeNo — not allowed
Only different parameter namesNo — names do not count

Common mistakes

  • Trying to overload by return type only — not allowed.
  • Writing two versions that are ambiguous, so no single best match exists.
  • Thinking different parameter names make an overload — only types/count matter.
  • Confusing overloading (compile-time) with overriding (run-time, via virtual functions).
🏋️ Practice

Write an overloaded area function: one for a square (one side), one for a rectangle (length and width), and one for a circle (radius, returning a double). Call each from main.

Summary

  • Overloading = several functions with the same name, different parameters.
  • The compiler picks the version that best matches your arguments.
  • Overloads must differ in parameter count, types or order.
  • You cannot overload by return type or parameter names alone.
  • Overloading is compile-time; overriding is a separate run-time concept.

Frequently Asked Questions

What is function overloading in C++?
Function overloading means defining several functions with the same name but different parameter lists — a different number of parameters or different types. The compiler picks the right version based on the arguments you pass, so one name can handle several kinds of input.
Why is function overloading useful?
It lets related operations share one intuitive name instead of inventing names like addInt, addDouble and addThree. This makes code cleaner and easier to read, since add(...) works whether you pass two ints, two doubles or three numbers.
How does the compiler decide which overloaded function to call?
The compiler looks at the number and types of the arguments in your call and matches them to the best-fitting version. This is called overload resolution. If no version fits, or two fit equally well, the compiler reports an error.
Can functions be overloaded by return type alone in C++?
No. Two functions cannot differ only by return type — the parameter lists must differ. Since the caller does not have to use the return value, the compiler could not tell the versions apart, so C++ does not allow overloading on return type alone.
What is the difference between overloading and overriding in C++?
Overloading is having several same-named functions with different parameters, resolved at compile time. Overriding is redefining a base class's virtual function in a derived class with the same signature, resolved at run time through polymorphism. They are different mechanisms despite sounding similar.

Function overloading क्या है?

Function overloading आपको कई functions को एक ही नाम देने देता है, बशर्ते उनकी parameter सूचियाँ भिन्न हों — अलग संख्या में parameters, या अलग types। C++ आपके भेजे arguments से पता कर लेता है कि आपका मतलब कौन-सा था।

Overload क्यों करें?

Overloading के बिना आपको एक ही विचार के लिए भद्दे नाम चाहिए: addInts, addDoubles, addThree। Overloading एक साफ़ नाम — add — को इन सबकी सेवा करने देता है, तो callers को variants याद नहीं रखने पड़ते।

एक चलता उदाहरण

C++
#include <iostream>
using namespace std;

int add(int a, int b)          { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c)   { return a + b + c; }

int main() {
    cout << add(2, 3) << "\n";        // int version
    cout << add(2.5, 3.5) << "\n";    // double version
    cout << add(1, 2, 3) << "\n";     // teen-arg version
    return 0;
}
Output:
5
6
6

Compiler कैसे चुनता है

Compiler आपके call को उस version से मिलाता है जिसके parameters argument types और संख्या से सबसे अच्छे फिट हों — इस प्रक्रिया को overload resolution कहते हैं। उदाहरण में, add(2, 3) दो-int version से मेल खाता है, add(2.5, 3.5) दो-double version से, और ऐसे ही।

अलग क्या माना जाता है

अंतरमान्य overload?
Parameters की अलग संख्याहाँ
अलग parameter typesहाँ
Types का अलग क्रमहाँ (जैसे (int,double) बनाम (double,int))
केवल अलग return typeनहीं — अनुमति नहीं
केवल अलग parameter नामनहीं — नाम मायने नहीं रखते

आम गलतियाँ

  • केवल return type से overload करने की कोशिश — अनुमति नहीं।
  • दो ऐसे versions लिखना जो ambiguous हों, तो कोई एकल सर्वोत्तम मेल न हो।
  • यह सोचना कि अलग parameter नाम overload बनाते हैं — केवल types/संख्या मायने रखते हैं।
  • Overloading (compile-time) को overriding (run-time, virtual functions से) से भ्रमित करना।
🏋️ अभ्यास

एक overloaded area function लिखें: एक square के लिए (एक भुजा), एक rectangle के लिए (लंबाई और चौड़ाई), और एक circle के लिए (त्रिज्या, double लौटाते हुए)। हर एक को main से call करें।

सारांश

  • Overloading = एक ही नाम वाले कई functions, अलग parameters।
  • Compiler वह version चुनता है जो आपके arguments से सबसे अच्छा मेल खाए।
  • Overloads को parameter संख्या, types या क्रम में भिन्न होना चाहिए।
  • आप केवल return type या parameter नामों से overload नहीं कर सकते।
  • Overloading compile-time है; overriding एक अलग run-time अवधारणा है।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.