Function overloading: Difference between revisions

Content deleted Content added
Add reference & over loadings are distinguished by type signature, which can include return type (as in Ada) or just parameters (as in C++). More impartial phrasing. Still much to do...
m Disambiguating links to Object-orientation (link changed to Object-oriented programming) using DisamAssist.
 
(10 intermediate revisions by 9 users not shown)
Line 9:
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}}
Line 34 ⟶ 37:
* [[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_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 have different [[Typetype signature|type signatures]]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
| last1 = Watt
| first1 = David A.
| first1 = David A.
| last2 = Findlay
| last2 = Findlay
| first2 = William
| first2 = William
| title = Programming Language Design Concepts
| date = 1 May 2004
| date = 1 May 2004
| publisher = John Wiley & Sons, Inc.
| isbn = 978-0-470-85320-7
| pages = 204–207
| pages = 204-207
}}
</ref>
 
Function overloading is usually associated with [[statically-typed]] programming languages that enforce [[type checking]] in [[function call]]s. OverloadedAn overloaded function is really just a set of different functions that happenare tocallable havewith the same name. For any particular call, the compiler determines which overloaded function to use, and resolves this 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}}
 
Sometime overloading wrongly taken as 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 or/and number of parameters.
The mistake comed from confusion in each form of static polymorphism as following:
* ''[[Parametric polymorphism]]'' differs from overloading by that the function definition exists only ones and this function invoked with parameters of different types which are substituted formal parameter types.
* ''[[Ad hoc polymorphism]]'' is very similar to overloading but it's depends of a language. This classification was introduced by [[Christopher Strachey]] in 1967, but difference is that he applied for compiler specific features which was exists in some programming languages. It was possibly to define in a module only name of a function and have in another module a multiple functions with same name(exact [[Function overloading|function overloading]]) so one module doesn't have access to another module until compiled. Thus was created effect that in first module was used same function for different parameters but it was just a cosmetic effect therefore [[Christopher Strachey]] gave it such special name as "ad hoc". But not any language has same feature, e.g. in [[Java (programming language)|Java]], there is no possibility define such function, similar example in Java can be achieved by using common interface for both modules but interface should have exact function signature thus ''[[Ad hoc polymorphism]]'' can't be applied.
 
Function overloading differs from forms of [[Polymorphism (computer science)|polymorphism]] where the choice is made at runtime, e.g. through [[virtual function]]s, instead of statically.
Line 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 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.