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...
Line 37:
 
==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 [[Type signature|type signatures]], 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
* The functions must differ either by the [[arity]] or types of their parameters
| last1 = Watt
| first1 = David A.
| last2 = Findlay
| first2 = William
| 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 overloadedOverloaded function is really just a set of different functions that happen to have 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 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++'''