How does function overloading help? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 12

How does function overloading help?

How does implementing function overloading help in a program? What are its benefits?

1st Oct 2018, 1:37 PM
Navoneel
4 Answers
+ 8
It prevents us from having to use destinct names for a function that fullfills the same purpose. Instead of having addInt(int a, int b) and addFloat(float a, float b), we can have just one function name: add(int a, int b) and add(float a, float b).
1st Oct 2018, 2:20 PM
Division by Zero
+ 4
In C#, this was the old way or may be the simple way to have functions accept different types of arguments such as int, float, double...etc. but this is not very useful now days because of generics, where u can have one method take different types
2nd Oct 2018, 2:47 AM
jay
+ 3
An overloaded function is really just a set of different functions that happen to have the same name. The determination of which function to use for a particular call is resolved at compile time. In the Java programming language, function overloading is also known as compile-time polymorphism and static polymorphism. https://www.geeksforgeeks.org/function-overloading-c/
2nd Oct 2018, 10:54 AM
deepak sharma
deepak sharma - avatar
+ 2
Function refers to a segment that groups code to perform a specific task. Example 1: Function Overloading #include <iostream> using namespace std; void display(int); void display(float); void display(int, float); int main() { int a = 5; float b = 5.5; display(a); display(b); display(a, b); return 0; } void display(int var) { cout << "Integer number: " << var << endl; } void display(float var) { cout << "Float number: " << var << endl; } void display(int var1, float var2) { cout << "Integer number: " << var1; cout << " and float number:" << var2; } Output Integer number: 5 Float number: 5.5 Integer number: 5 and float number: 5.5 Here, the display() function is called three times with different type or number of arguments. The return type of all these functions are same but it's not necessary. Example 2: Function Overloading // Program to compute absolute value // Works both for integer and float #include <iostream> using namespace std; int absolute(int); float absolute(float); int main() { int a = -5; float b = 5.5; cout << "Absolute value of " << a << " = " << absolute(a) << endl; cout << "Absolute value of " << b << " = " << absolute(b); https://crbtech.in/java-training/best-advanced-java-training-institutes-pune-top-certification/
3rd Oct 2018, 9:16 AM
meenal deshpande