Function overloading: Difference between revisions

Content deleted Content added
I have updated the content of method or function overloading. If anybody can give the description on this topic briefly, he or she can edit in this or contribute. For contact, Email: soumyadipmajumder03@outlook.com
Tag: Reverted
m Disambiguating links to Object-orientation (link changed to Object-oriented programming) using DisamAssist.
 
(28 intermediate revisions by 24 users not shown)
Line 4:
{{refimprove|date=October 2011}}
}}
{{Confuse|Instruction overlapping|Overlay (programming)|Method overriding}}
{{Polymorphism}}
 
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.
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'?
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__
class example {
public int multiplication(int x, int y) {
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;
}
}
 
== Basic definition ==
public class main
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.
{
public static void main(String[] args)
{
example q = new example();
System.out.println(q.multiply(10,20));
System.out.println(q.multiply(5,4,5));
System.out.println(q.multiply(10.5,20.5));
}
}
 
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)}}.
→ 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. As per argument passing the answer shows all types of results.
 
# Important: I have written this programming language in 'JAVA'. So, anybody who sees this if he or she use on different programming language or different coding platform, don't worry. Concept is same just syntax can be different.
==Languages supporting overloading==
 
Languages which support function overloading include, but are not necessarily limited to, the following:
 
* [[Ada (programming language)|Ada]]
* [[Apex (programming language)|Apex]]
* [[C++]]
* [[C Sharp (programming language)|C#]]
* [[Clojure]]<ref>{{Cite web |title=Clojure - Learn Clojure - Functions |url=https://clojure.org/guides/learn/functions#_multi_arity_functions |access-date=2023-06-13 |website=clojure.org}}</ref>
* [[D (programming language)|D]]
* [[Swift (programming language)|Swift]]
* [[Fortran]]
* [[Kotlin (programming language)|Kotlin]]<ref>{{cite web |title=Kotlin language specification |url=https://kotlinlang.org/spec/overload-resolution.html |website=kotlinlang.org}}</ref>
* [[Java (programming language)|Java]]{{sfn|Bloch|2018|loc=§Chapter 8 Item 52: Eliminate unchecked warnings|p=238-244}}
* [[Julia (programming language)|Julia]]
* [[PostgreSQL]]<ref>{{Cite web|date=2021-08-12|title=37.6. Function Overloading|url=https://www.postgresql.org/docs/13/xfunc-overload.html|access-date=2021-08-29|website=PostgreSQL Documentation|language=en}}</ref> and [[PL/SQL]]<ref>{{Cite web|title=Database PL/SQL User's Guide and Reference|url=https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/subprograms.htm#i12352|access-date=2021-08-29|website=docs.oracle.com|language=en}}</ref>
* [[Scala (programming language)|Scala]]
* [[TypeScript]]
* [[Visual Basic (.NET)]]
* [[Wolfram Language]]
* [[Elixir (programming language)|Elixir]]
* [[Nim (programming language)|Nim]]<ref>{{cite web|title=Nim Manual|url=https://nim-lang.org/docs/manual.html#overloading-resolution|website=nim-lang.org|language=en}}</ref>
* [[Crystal (programming language)|Crystal]]<ref>{{cite web|title=Crystal Docs|url=https://crystal-lang.org/reference/1.8/syntax_and_semantics/overloading.html|website=crystal-lang.org|language=en}}</ref>
* [[Delphi (software)|Delphi]]<ref>{{cite web|title=Embarcadero Delphi|url=https://docwiki.embarcadero.com/RADStudio/Athens/en/Overloading|website=embarcadero.com|language=en}}</ref>
* [[Python (programming language)|Python]]
 
Languages that do not support function overloading include [[C (programming language)|C]], [[Rust (programming language)|Rust]] and [[Zig (programming language)|Zig]].
 
==Rules in function overloading==
* The same function name is used for more than one function definition in a particular module, class or namespace
* The functions must differhave either by thedifferent [[aritytype signature]]s, i.e. differ in the number or the types of their formal parameters (as in C++) or additionally in their return type (as in Ada).<ref>{{cite book
| last1 = Watt
 
| first1 = David A.
It is a classification of static polymorphism in which a function call is resolved using some "best match" algorithm, where the particular function to call is resolved by finding the best match of the formal parameter types with the actual parameter types. The details of this algorithm vary from language to language.
| last2 = Findlay
 
| first2 = William
Function overloading is usually associated with [[statically-typed]] programming languages that enforce [[type checking]] in [[function call]]s. 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]].
| title = Programming Language Design Concepts
| date = 1 May 2004
| publisher = John Wiley & Sons, Inc.
| isbn = 978-0-470-85320-7
| pages = 204–207
}}
</ref>
 
Function overloading is usually associated with [[statically-typed]] programming languages that enforce [[type checking]] in [[function call]]s. An overloaded function is really just a set of different functions that happenare tocallable havewith the same name. TheFor determinationany ofparticular call, the compiler determines which overloaded function to use forand aresolves particular call is resolvedthis at [[compile time]]. This is true for programming languages such as Java.{{sfn|Bloch|2018|loc=§Chapter 8 Item 52: Use overloading judiciously|p=238-244}}
In [[Java (programming language)|Java]], function overloading is also known as compile-time polymorphism and static polymorphism.
 
Function overloading shoulddiffers not be confused withfrom forms of [[Polymorphism (computer science)|polymorphism]] where the choice is made at runtime, e.g. through [[virtual function]]s, instead of statically.
 
'''Example: Function overloading in C++'''
Line 92 ⟶ 87:
 
==Constructor overloading==
[[Constructor (object-oriented programming)|Constructors]], used to create instances of an object, may also be overloaded in some [[Object-oriented programming|object-oriented]] [[programming language]]s. Because in many languages the constructor's name is predetermined by the name of the class, it would seem that there can be only one constructor. Whenever multiple constructors are needed, they are to be implemented as overloaded functions. In [[C++]], [[default constructor]]s take no parameters, instantiating the object [[Instance variable|members]] with their appropriate default values, "which is normally zero for numeral fields and empty string for string fields".<ref>{{cite book |last1=Chan |first1=Jamie |title=Learn C# in One Day and Learn It Well |date=2017 |isbn=978-1518800276 |page=82 |edition=Revised}}</ref> For example, a default constructor for a restaurant bill object written in C++ might set the tip to 15%:
 
<syntaxhighlight lang=Cpp>
Line 131 ⟶ 126:
Two issues interact with and complicate function overloading: [[Name masking]] (due to [[Scope (computer science)|scope]]) and [[implicit type conversion]].
 
If a function is declared in one scope, and then another function with the same name is declared in an inner scope, there are two natural possible overloading behaviors: the inner declaration masks the outer declaration (regardless of signature), or both the inner declaration and the outer declaration are both included in the overload, with the inner declaration masking the outer declaration only if the signature matches. The first is taken in C++: "in C++, there is no overloading across scopes."<ref name=bjarne_faq>{{cite web |url=http://www.stroustrup.com/bs_faq2.html#overloadderived| title=Why doesn't overloading work for derived classes? |last=Stroustrup |first=Bjarne |author-link=Bjarne Stroustrup}}</ref> As a result, to obtain an overload set with functions declared in different scopes, one needs to explicitly import the functions from the outer scope into the inner scope, with the {{code|using}} keyword.
 
Implicit type conversion complicates function overloading because if the types of parameters do not exactly match the signature of one of the overloaded functions, but can match after type conversion, resolution depends on which type conversion is chosen.
Line 162 ⟶ 157:
* [[Abstraction (computer science)]]
* [[Constructor (computer science)]]
* [[Default argument]]
* [[Dynamic dispatch]]
* [[Factory method pattern]]
Line 169 ⟶ 165:
* [[Operator overloading]]
 
==ReferencesCitations==
{{Reflist}}
 
==References==
*{{cite book | title= "Effective Java: Programming Language Guide" |last=Bloch| first=Joshua| publisher=Addison-Wesley | edition=third | isbn=978-0134685991| year=2018}}
 
==External links==