Function overloading: Difference between revisions

Content deleted Content added
short description, formatting, links
Tags: Mobile edit Mobile app edit iOS app edit
No edit summary
Tag: Reverted
Line 7:
{{Polymorphism}}
 
In programming language 'Method overloading' or you can say 'Function overloading' is very important concept to apply in real life entities. First of all Method Overloading or Function Overloading is a very important concept part of OOPs(Object Oriented Programming). It is based on polymorphism. The word 'Polymorphism' means a same person or an same object is represented like various kinds of things as per situation, like a person is called as an Employee, as a son, as a bhaiya, as a bhai is called in different situation. So how we determine what is the method or function 'overloading'?
In some [[programming language]]s, '''function overloading''' or '''method overloading''' is the ability to create multiple [[subprogram|functions]] of the same name with different implementations. Calls to an overloaded function will run a specific implementation of that function appropriate to the context of the call, allowing one function call to perform different tasks depending on context.
Ans: The function or method overloading is a concept that allows different methods having different type of parameters having the same name of more than one method and also having the different signatures.(Signatures represent the difference is represented by the number of input parameters and type of parameters).
 
__Let's take an example__
For example, {{mono|doTask()}} and {{nowrap|{{mono|doTask(object o)}}}} are overloaded functions. To call the latter, an [[object (computer science)|object]] must be passed as a [[parameter (computer science)|parameter]], whereas the former does not require a parameter, and is called with an empty parameter field. A common error would be to assign a default value to the object in the second function, which would result in an ''ambiguous call'' error, as the [[compiler]] wouldn't know which of the two methods to use.
class example {
 
public int multiplication(int x, int y) {
Another example is a {{nowrap|{{mono|Print(object o)}}}} function that executes different actions based on whether it's printing text or photos. The two different functions may be overloaded as {{nowrap|{{mono|Print(text_object T); Print(image_object P)}}.}} If we write the overloaded print functions for all objects our program will "print", we never have to worry about the type of the object, and the correct function call again, the call is always: {{mono|Print(something)}}.
return x*y;
 
}
public int multiplication(int x, int y, int z) {
return x*y*z;
}
public double multiplication(double x, double y) {
return x*y;
}
}
→ NOTE: Here, the name of the method or function is 'multiplication'. But this method has different no. of parameters and different datatype of parameters. So, as per user input the all method works properly. Method overloading can be achieved by changing the number of parameters while passing to different methods.
==Languages supporting overloading==