Create overloaded routines

The database server can support routine overloading because it supports polymorphism: the ability to have many entities with the same name and to choose the entity most relevant to a particular usage.

You can have more than one routine with the same name but different parameter lists, as in the following situations:
  • You create a routine with the same name as a built-in function, such as equal(), to process a new UDT.
  • You create type hierarchies, in which subtypes inherit data representation and functions from supertypes.
  • You create distinct types, which are data types that have the same internal storage representation as an existing data type, but have different names. Distinct types cannot be compared to the source type without casting. Distinct types inherit UDRs from their source types.
For example, you might create each of the following user-defined functions to calculate the area of different data types (each data type represents a different geometric shape):
CREATE FUNCTION area(arg1 circle) RETURNING DECIMAL...
CREATE FUNCTION area(arg1 rectangle) RETURNING DECIMAL....
CREATE FUNCTION area(arg1 polygon) RETURNING DECIMAL....

These three CREATE FUNCTION statements create an overloaded routine called area(). Each CREATE FUNCTION statement registers an area() function for a particular argument type. You can overload a routine so that you have a customized area() routine for every data type that you want to evaluate.

The advantage of routine overloading is that you do not need to invent a different name for a routine that performs the same task for different arguments. When a routine has been overloaded, the database server can choose which routine to execute based on the arguments of the routine when it is invoked.