Overloading Subprograms v11
Generally, subprograms of the same type (subprocedure or subfunction) with the same name, and same formal parameter specification can appear multiple times within the same standalone program as long as they are not sibling blocks (that is, the subprograms are not declared in the same local block).
Each subprogram can be individually invoked depending upon the use of qualifiers and the location where the subprogram invocation is made as discussed in the previous sections.
It is however possible to declare subprograms, even as siblings, that are of the same subprogram type and name as long as certain aspects of the formal parameters differ. These characteristics (subprogram type, name, and formal parameter specification) is generally known as a program’s signature.
The declaration of multiple subprograms where the signatures are identical except for certain aspects of the formal parameter specification is referred to as subprogram overloading.
Thus, the determination of which particular overloaded subprogram is to be invoked is determined by a match of the actual parameters specified by the subprogram invocation and the formal parameter lists of the overloaded subprograms.
Any of the following differences permit overloaded subprograms:
- The number of formal parameters are different.
- At least one pair of data types of the corresponding formal parameters (that is, compared according to the same order of appearance in the formal parameter list) are different, but are not aliases. Data type aliases are discussed later in this section.
Note that the following differences alone do not permit overloaded subprograms:
- Different formal parameter names
- Different parameter modes
(IN, IN OUT, OUT)
for the corresponding formal parameters - For subfunctions, different data types in the
RETURN
clause
As previously indicated, one of the differences allowing overloaded subprograms are different data types.
However, certain data types have alternative names referred to as aliases, which can be used for the table definition.
For example, there are fixed length character data types that can be specified as CHAR
or CHARACTER
. There are variable length character data types that can be specified as CHAR VARYING, CHARACTER VARYING, VARCHAR,
or VARCHAR2
. For integers, there are BINARY_INTEGER, PLS_INTEGER,
and INTEGER
data types. For numbers, there are NUMBER, NUMERIC, DEC,
and DECIMAL
data types.
For detailed information about the data types supported by Advanced Server, see Data types.
Thus, when attempting to create overloaded subprograms, the formal parameter data types are not considered different if the specified data types are aliases of each other.
It can be determined if certain data types are aliases of other types by displaying the table definition containing the data types in question.
For example, the following table definition contains some data types and their aliases.
Using the PSQL \d
command to display the table definition, the Type column displays the data type internally assigned to each column based upon its data type in the table definition:
In the example, the base set of data types are bytea, integer, real, double precision, numeric, character
, and character varying
.
When attempting to declare overloaded subprograms, a pair of formal parameter data types that are aliases would not be sufficient to allow subprogram overloading. Thus, parameters with data types INTEGER
and PLS_INTEGER
cannot overload a pair of subprograms, but data types INTEGER
and REAL
, or INTEGER
and FLOAT
, or INTEGER
and NUMBER
can overload the subprograms.
Note
The overloading rules based upon formal parameter data types are not compatible with Oracle databases. Generally, the Advanced Server rules are more flexible, and certain combinations are allowed in Advanced Server that would result in an error when attempting to create the procedure or function in Oracle databases.
For certain pairs of data types used for overloading, casting of the arguments specified by the subprogram invocation may be required to avoid an error encountered during runtime of the subprogram. Invocation of a subprogram must include the actual parameter list that can specifically identify the data types. Certain pairs of overloaded data types may require the CAST
function to explicitly identify data types. For example, pairs of overloaded data types that may require casting during the invocation are CHAR
and VARCHAR2
, or NUMBER
and REAL
.
The following example shows a group of overloaded subfunctions invoked from within an anonymous block. The executable section of the anonymous block contains the use of the CAST
function to invoke overloaded functions with certain data types.
The following is the output displayed from the anonymous block: