⚙️ C++ Beginner

What is function overloading in C++?

Why Interviewers Ask This

Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for C++ development. It reveals whether you understand the building blocks that more complex concepts rely on.

Answer

Function overloading allows multiple functions to have the same name but different parameter lists (different number, types, or order of parameters). The compiler selects the correct version at compile time — this is compile-time polymorphism. void print(int n) { std::cout << "int: " << n << "\n"; } void print(double d) { std::cout << "double: " << d << "\n"; } void print(std::string s) { std::cout << "string: " << s << "\n"; } void print(int n, std::string label) { std::cout << label << ": " << n << "\n"; } // Calls: print(42); // int version print(3.14); // double version print("hello"); // string version print(42, "count"); // two-param version. Rules for overloading: functions must differ in parameter types, number, or order — NOT just return type. int foo(); double foo(); is INVALID. How the compiler resolves: exact match → promotion (int→double) → standard conversion → user-defined conversion → variadic. Ambiguous calls (multiple equally good matches) are compile errors. Name mangling: C++ compiler encodes function name + parameter types in the compiled symbol name — this is how multiple overloads coexist. Use extern "C" to disable mangling for C interoperability. Default arguments (related): void print(int n, int base = 10) — default arguments reduce the need for some overloads. Default args must be at the end of the parameter list and can't be specified in both declaration and definition. Overloading vs overriding: overloading = same name, different params, same or different class (compile-time); overriding = same name, same params, derived class virtual function (runtime).

Common Mistake

Don't just define the term — demonstrate that you understand when to use it and when not to. Showing awareness of trade-offs is what separates average from strong C++ candidates.