Function overloading: Difference between revisions

Content deleted Content added
short description, formatting, links
Tags: Mobile edit Mobile app edit iOS app edit
m Disambiguating links to Object-orientation (link changed to Object-oriented programming) using DisamAssist.
 
(30 intermediate revisions by 25 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.
 
== Basic definition ==
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.
 
Line 17 ⟶ 18:
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 69 ⟶ 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 108 ⟶ 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 139 ⟶ 157:
* [[Abstraction (computer science)]]
* [[Constructor (computer science)]]
* [[Default argument]]
* [[Dynamic dispatch]]
* [[Factory method pattern]]
Line 146 ⟶ 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==