What is the use/advantage of function overloading?

IMHO the benefit is consistency in the naming of functions which perform the same task, just with different parameters.

Multiple behaviors to the same function based on the parameters. Your function may want to work with some optional details. For example, the following example wants to add a member to the Members object, with whatever detail the user knows.

Here age is the minimum detail to create a member, age and memberOf are optional. Note: definition of functions are not provided in the code snippet. Public class Members { public System.Collections.Generic.

List TeamMembers; public AddMember(Member m) {} public AddMember(string name) {} public AddMember(string name, int age) {} public AddMember(string name, int age, string memberOf) {} public class Member { public string Name { get; set; } public int Age { get; set; } public string MemberOf { get; set; } } } You may want your method to be suitable for multiple type of objects. Ex. Console.WriteLine() method is capable of writing empty line, bool, int, string, char, float etc.On the console.

This was made possible because of function overloading.

I don't see this as good use of function overloading, especially as you have to either duplicate code or invoke the simpler methods (with less arguments) inside of the more complex ones. – Davorin 2 days ago Default parameter can provide answer to addition of more parameters. But the essence here is method overloading lets you execute an entirely different block of code (method) based on the type of the parameter and the number of parameters being inserted.

In the first two variations of AddMember, you can add a Member object as an argument or a string; the execution of them might go in different route. Compare this functionality with Console.Write() method for instance. – SaravananArumugam 2 days ago.

A function/method is sometimes able to take different kinds of parameters in order to do it's job. This is the time for function overloading. Otherwise, you would have to have different functions for the same functionality, which is confusing and bad practice.

Constructors are functions, so they can be overloaded. This is very handy. When you first get into overloading, it's easy to get over-fancy, thinking you're doing future developers a favor by giving them more convenient options.

Try to avoid this. Unneeded overloads can confuse future developers and cause unnecessary code to maintain.

It providing multiple behaviour to same object with respect to attributes of object. For example a method called addUs(a b) adds a and b. So the definition will be: int addUs(int a int b){ return a+b; } But now if you want your arguments to be Objects of a class say: class Demo{ int height; int width; } You want the same function addUs() to return a new object which would have attributes height and width having values as sum of the height & width of the 2 arguments passed.So now the definition would be: Demo addUs(Demo a Demo b){ this.

Height a. Height + b. Height; this.

Widht a. Widht + b. Width; return this; }.

Sometimes you have multiple ways of accomplishing the same thing based on the context and inputs available. For type-strict static languages the function definitions can be pretty rigid and need to be explicitly defined ahead of time. Constructors are generally the best classic example of this.

If you're building a complex object and don't have all the pieces, you still want to be able to pass what you have to a constructor and let it fill in the rest. And what you have may vary wildly and need to be defined in different ways as parameters to the constructors.

Very valid question. You get consistency in naming, but at the cost of ambiguity about exact implementation. The real issue is human memory for method names, right?

We find it easier to remember names that are commonly used. And economy of typing, allowing for shorter method names? Fewer different names means (mathematically) that the name itself carries less information.

These two issues shouldn't be any concern, with IDEs that find/guess/insert method names rapidly based on the first few characters, and parameter/return types. But I do think there is a cost, in preciseness of coding, as well as a benefit.

You might want to do similar things in code with different parameters. If you had to give every function a different name, code readability would be very bad.

Overloading is a form of polymorphism. It allows the programmer to write functions to do conceptually the same thing on different types of data without changing the name. (It also allows the programmer to write functions to do conceptually different things depending on parameters, but that's a Real Bad Idea.) This allows consistency in notation, which is good both for reading and for writing code.

I/O is a very common use. In most languages in common use, there's a function or operator that will output whatever you like, such as printf() and kin in C, operatorLanguages that require functions like printint(), printstring(), printfloat(), and the like have never caught on. It works very well with C++ templates and any other construct where you don't necessarily know what the variable type is at the time of writing the code.

The use is interpreted as transformative, as opposed to merely derivative.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions