Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Why is it impossible to overload functions with distinct return types?
3.
Example Code
3.1.
Output
4.
Frequently Asked Questions
4.1.
What is the definition of function overloading?
4.2.
What are the circumstances under which a function can be overloaded?
4.3.
Does C support function overloading in the same way that C++ and Java do?
4.4.
How does function overloading help with memory management?
4.5.
What is the purpose of overloading functions?
5.
Conclusion
Last Updated: Mar 27, 2024

Function overloading and return type

Author Manan Singhal
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In C++ and Java, function overloading is feasible, but only if the functions must differ in type and number of arguments in the argument list. Overloading is impossible if the only difference between functions is the return type.

Why is it impossible to overload functions with distinct return types?

Compile-time polymorphism includes function overloading. The function signature is examined during compilation. If the signatures aren't the same, functions can be overloaded. Function overloading is unaffected by a function's return type. Therefore, the same function signature with a different return type will not be overloaded.


You can also practice with the help of Online C++ Compiler

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Example Code

/* CPP program to show that function overloading fails if only return types are different */
#include <iostream>

/* Function returning int */
int fun() {
    return 10;
}

/* Function returning char */
char fun() {
    return 'a';
}
  
/* Main Code */
int main() {
    char x = fun();
    getchar();
    return 0;
}

Output

10 | char fun() {
      |      ^~~
main.cpp:5:5: note: old declaration 'int fun()'
    5 | int fun() {
      |     ^~~

Error: ambiguating new declaration of ‘char fun()’.

Check out this article - Compile Time Polymorphism

Frequently Asked Questions

What is the definition of function overloading?

C++ has a function overloading feature, which allows two or more functions to have the same name but distinct parameters.

What are the circumstances under which a function can be overloaded?

More than one function definition uses the same function name, and the arity or types of the parameters must be different between the functions.

Does C support function overloading in the same way that C++ and Java do?

C does not support function overloading in the same way that C++ supports function overloading, as well as Java supports function overloading.

How does function overloading help with memory management?

Function overloading never saves space since the compiler builds two separate versions of the function and calls one of them depending on the call. Overloading functions makes your code more readable. This is similar to overloading, but the above code is easier to comprehend.

What is the purpose of overloading functions?

The fundamental benefit of function overloading is that it makes code more readable and allows it to be reused. Function overloading is used to reduce memory space, maintain consistency, and improve readability. It accelerates the program's execution. Maintenance of the code is also simplified.

Conclusion

In this article, we have learned the function overloading and return type in C++. We hope that this blog will help you understand the concept of function overloading, and if you would like to learn more about it, check out our other blogs on function overloading and function overridingfunction overloading, and understanding function overloading In C++ with examples.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Previous article
Const keyword in C++
Next article
Overloading With Inheritance
Live masterclass