Content deleted Content added
m Disambiguating links to Object-orientation (link changed to Object-oriented programming) using DisamAssist. |
|||
(25 intermediate revisions by 21 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:
* [[Apex (programming language)|Apex]]
▲* [[C (programming language)|C]]
* [[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
| last1 = Watt
| first1 = David A.
| 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
Function overloading
'''Example: Function overloading in C++'''
Line 71 ⟶ 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 110 ⟶ 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
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 141 ⟶ 157:
* [[Abstraction (computer science)]]
* [[Constructor (computer science)]]
* [[Default argument]]
* [[Dynamic dispatch]]
* [[Factory method pattern]]
Line 148 ⟶ 165:
* [[Operator overloading]]
==
{{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==
|