Reputation: 13275
Lately I was struggling to explain the way a certain method works to a colleague of mine. The problem was related to me not knowing a certain term (that probably exists). Take a function like this:
function myFunct (arg) {
if (typeof arg == "number") {
// ...
}
if (typeof arg == "string") {
// ...
}
}
Depending on the datatype of arg
, the method does something different. What is the correct term for such a function that accepts its argument in different data types?
Upvotes: 2
Views: 76
Reputation: 16172
This is called "polymorphism", here is the definition from wikipedia:
... polymorphic functions that can be applied to arguments of different types, but that behave differently depending on the type of the argument to which they are applied (also known as function overloading or operator overloading)
In statically-typed languages, like C++, it is possible define multiple functions with the same name, but different arguments. For example myFunct(int arg)
and myFunct(string arg)
.
In dynamically-typed languages, like php or python, the function can accept a parameter of any type and do things differently depending on the type like in your example.
Upvotes: 2