Polymorphic functions .
. Strongly typed languages can make programming very tedious
. Consider identity function written in a language like Pascal
function identity (x: integer): integer;
. This function is the identity on integers
identity: int int
. In Pascal types must be explicitly declared
. If we want to write identity function on char then we must write
function identity (x: char): char;
. This is the same code; only types have changed. However, in Pascal a new identity function must be written for each type
The above example tells us that how tedious it can be to code in strongly typed languages. Polymorphism gives the user flexibility in coding. The user can use just one function declaration to handle multiple input types. Otherwise (s)he will have to write a separate function for each input type as in Pascal.
|