Why would I want to overload function ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why would I want to overload function ?

Isn't it a something that could potentially confuse me or someone who would check my code ? Isn't it better to name function similiar like ThatOneFunctionA and next one ThatOneFunctionB instead of overloading ? That is a little confusing for me ... P.S.: I understand that some cases could use a float instead of an integer but that is at least one case that I am able to think of.

16th Nov 2016, 7:59 PM
Michał Wojciechowski
Michał Wojciechowski - avatar
5 Answers
+ 7
Overloading allows you to have multiple types for input. Take the following example: int sum(int a, int b) { return a + b } // But wait! // This only works with whole numbers. double sum(double a, double b) { return a + b } // Now, I can call them with integers, // or doubles.
16th Nov 2016, 8:22 PM
Keto Z
Keto Z - avatar
+ 3
Overloading is for if you have the same thing done, but with different arguments or a different type of argument. So, you can use the same function but without including a certain argument for example if you do not need it. You do not have to overload but it can be very, very helpful. Keep in mind that a lot of built in functions use overloading, as is indicated by the fact that certain arguments are optional.
16th Nov 2016, 8:25 PM
NICKALL [EP]
NICKALL [EP] - avatar
+ 1
If you are familiar with software development, the most difficult task is the software maintenance. The code should be highly readable in order to be maintainable. It's more effective to declare different functions that have familiar functionality with the same name than declaring different functions with different names. This may confuse the reviewers each time they come across a different version of your function. When they skim your code they want to understand what you are doing as quickly as possible and not bother themselves with questions like "What does this B in the name of the function stands for? Does the function perform something completely different from what its name indicates?". It may not make sense to you right now, but if you move deeper into the software development industry and you start writing bigger software products with many interconnected classes, you will realize the importance of keeping your code as simple (not verbose) as possible.
16th Nov 2016, 8:32 PM
Miltiadis Siavvas
+ 1
It allows you to use the same function with different inputs. A great example I learned is te built in methods in the langue. For example the Console.Write method of C#. This method is overloaded so no matter what you type in this method it works and you do not have to consider what (correct) input you are sending the method.
16th Nov 2016, 9:02 PM
Eric Farmer
Eric Farmer - avatar
+ 1
You can also have a function that needs no arguments passed and has a default behavior. Many frameworks are good example of the use and usefulness of overloading, where you can write the function call with or without arguments being passed to the function. Example: doSomething( );//Does something or doSomething( 3 ); //does something 3 times or doSomething( 2, true, "John" );//Does something 2 times, sets some variable to true and uses a string
24th Nov 2016, 3:31 AM
Israel
Israel - avatar