Advantages and Disadvantages of Function Overloading in C++
Function overloading is one of the important features of object-oriented programming. It allows users to have more than one function having the same name but different properties. Overloaded functions enable users to supply different semantics for a function, depending on the signature of functions.
Advantages of function overloading are as follows:
- The main advantage of function overloading is that it improves code readability and allows code reusability.
- The use of function overloading is to save memory space, consistency, and readability.
- It speeds up the execution of the program
- Code maintenance also becomes easy.
- Function overloading brings flexibility to code.
- The function can perform different operations and hence it eliminates the use of different function names for the same kind of operations.
Disadvantage of function overloading are as follows:
- Function declarations that differ only in the return type cannot be overloaded
int fun(); float fun();
It gives an error because the function cannot be overloaded by return type only.
- Member function declarations with the same name and the same parameter types cannot be overloaded if any of them is a static member function declaration.
- The main disadvantage is that it requires the compiler to perform name mangling on the function name to include information about the argument types.
Here is int 10 Here is float 10.1 Here is char* ten
In the above example all functions that were named the 3 same but printing different to which we can perceive there were different calls been made. This can be possible because of the arguments been passed according to which the function call is executed no matter be the functions are sharing a common name. Also, remember we can overload the function by changing their signature in return type. For example, here boolean print() function can be called for all three of them.