Sub topic 7
Overloading Subprograms in a Package
This chapter assumes you can write a package specification and body, and that you know what a subprogram's formal parameters are. Here you will learn how to give several subprograms the same name in one package, how the compiler picks between them, and which declarations are rejected or ambiguous.
What overloading is
Overloading means declaring two or more subprograms with the same name in the same scope. Each version has a different parameter list. A caller writes one name, and the compiler chooses the version that fits the arguments.
Overloading is available in packages, in local subprograms declared in the same block, and inside object types. It is not available for standalone procedures and functions, because a schema holds only one object per name. This is a common reason to put related routines in a package.
Typical uses:
- Accept the same logical input in several datatypes, such as a number or a date that both need to be turned into text.
- Offer a short form and a long form of one operation, for example with or without a format mask.
- Keep a single, memorable API name instead of
to_text_number,to_text_dateand so on.
A first example
The specification lists every overload. Each one needs its own declaration.
CREATE OR REPLACE PACKAGE fmt AS
FUNCTION to_text(p_value IN NUMBER) RETURN VARCHAR2;
FUNCTION to_text(p_value IN DATE) RETURN VARCHAR2;
FUNCTION to_text(p_value IN DATE, p_mask IN VARCHAR2) RETURN VARCHAR2;
END fmt;
/
CREATE OR REPLACE PACKAGE BODY fmt AS
FUNCTION to_text(p_value IN NUMBER) RETURN VARCHAR2 IS
BEGIN
RETURN TO_CHAR(p_value, 'FM999G999G990D00');
END to_text;
FUNCTION to_text(p_value IN DATE) RETURN VARCHAR2 IS
BEGIN
RETURN to_text(p_value, 'YYYY-MM-DD');
END to_text;
FUNCTION to_text(p_value IN DATE, p_mask IN VARCHAR2) RETURN VARCHAR2 IS
BEGIN
RETURN TO_CHAR(p_value, p_mask);
END to_text;
END fmt;
/The one-argument date version calls the two-argument version. That works without a forward declaration because the spec already declares all three. Overloads declared only in the body would need to appear before they are used, or have a forward declaration (see the earlier chapter on forward declarations).
Calling the package:
SET SERVEROUTPUT ON
BEGIN
DBMS_OUTPUT.PUT_LINE(fmt.to_text(1234567.5));
DBMS_OUTPUT.PUT_LINE(fmt.to_text(DATE '2026-11-28'));
DBMS_OUTPUT.PUT_LINE(fmt.to_text(DATE '2026-11-28', 'DD Mon YYYY'));
END;
/Each call matches exactly one declaration. The first resolves to the NUMBER version, the second to the DATE version with one parameter, and the third to the two-parameter version.
What makes two declarations different
The compiler tells overloads apart only by their formal parameters. The rules are:
| Difference between two declarations | Allowed as an overload? |
|---|---|
| Different number of parameters | Yes |
| Different datatype family in at least one position | Yes |
| Same types in a different order, with the types in at least one position from different families | Yes |
| Only the parameter names differ | No |
| Only the parameter modes differ (`IN`, `OUT`, `IN OUT`) | No |
| Only the return type differs | No |
| Types differ only within one family, such as `INTEGER` and `NUMBER`, or `CHAR` and `VARCHAR2` | No, calls are ambiguous |
A datatype family groups types that PL/SQL can convert between freely. NUMBER, INTEGER, PLS_INTEGER and FLOAT fall under the number family. CHAR, VARCHAR2 and LONG are character types in one family. DATE and TIMESTAMP are different families.
The practical consequence for names: parameter names do not affect overload resolution, but they matter when a caller uses named notation. Two overloads that use different names for the same position work, but make named calls harder to read.
Why return type alone does not work
A function call appears inside an expression, and PL/SQL does not choose a function from the type the surrounding expression expects. These two declarations cannot be told apart:
CREATE OR REPLACE PACKAGE bad_return AS
FUNCTION get_value(p_id IN NUMBER) RETURN NUMBER;
FUNCTION get_value(p_id IN NUMBER) RETURN VARCHAR2;
END bad_return;
/Give the functions different names, or add a parameter that distinguishes them.
How the compiler resolves a call
Resolution happens at compile time, not at run time. The compiler reads the actual arguments in the call, finds all declarations of that name that are visible, and compares them.
The compiler looks for the best match in this order:
- A declaration whose parameter types match the arguments exactly.
- If there is none, a declaration that matches after implicit conversions of the arguments.
- If more than one declaration still qualifies, the call is ambiguous and compilation fails.
Two error codes show up repeatedly:
PLS-00306: wrong number or types of arguments in call to '...'means no declaration fits.PLS-00307: too many declarations of '...' match this callmeans more than one declaration fits and the compiler will not guess.
Ambiguous calls in practice
Using the fmt package above, this block does not compile:
BEGIN
DBMS_OUTPUT.PUT_LINE(fmt.to_text(NULL));
END;
/A bare NULL has no type, so both the NUMBER and the DATE overloads fit. The call raises PLS-00307. The fix is to give the argument a type:
BEGIN
DBMS_OUTPUT.PUT_LINE(fmt.to_text(CAST(NULL AS NUMBER)));
END;
/A quoted string has the same problem. fmt.to_text('2026-11-28') provides a VARCHAR2, which can convert implicitly to either NUMBER or DATE. Do not rely on implicit conversion to choose an overload. Convert explicitly with TO_DATE or TO_NUMBER so that the argument has the type you intend.
Overloads that differ only within a family
Consider this specification:
CREATE OR REPLACE PACKAGE family_clash AS
PROCEDURE log_value(p_value IN INTEGER);
PROCEDURE log_value(p_value IN NUMBER);
END family_clash;
/INTEGER is a subtype of NUMBER, so any numeric argument fits both declarations, and callers get PLS-00307. If you need a different behavior for whole numbers, use a different procedure name, or add a parameter such as a flag.
Default values and named notation
Default parameter values can make two overloads match the same call.
CREATE OR REPLACE PACKAGE defaults_clash AS
PROCEDURE show(p_text IN VARCHAR2);
PROCEDURE show(p_text IN VARCHAR2, p_prefix IN VARCHAR2 DEFAULT '>');
END defaults_clash;
/A call defaults_clash.show('hello') fits both declarations, because the second can use its default. The compiler reports PLS-00307. Prefer a single subprogram with defaults over overloads that differ only by trailing defaulted parameters.
Named notation has a related trap. If two overloads share the same parameter name and type, a call that supplies only that name matches both.
Visibility: public and private overloads
Overloads follow the normal public and private rules.
- A subprogram declared in the specification is public. Every overload that callers should use must be in the specification.
- A subprogram declared only in the body is private. A private overload can share a name with a public one and take part in resolution, but only for calls made from inside the body.
- Inside the body, a call to a name sees both the public and the private declarations, so a call can resolve differently inside the package than outside it.
CREATE OR REPLACE PACKAGE shape AS
FUNCTION area(p_radius IN NUMBER) RETURN NUMBER;
END shape;
/
CREATE OR REPLACE PACKAGE BODY shape AS
-- Private overload: visible only inside this body.
FUNCTION area(p_width IN NUMBER, p_height IN NUMBER) RETURN NUMBER IS
BEGIN
RETURN p_width * p_height;
END area;
FUNCTION area(p_radius IN NUMBER) RETURN NUMBER IS
BEGIN
RETURN 3.14159 * p_radius * p_radius;
END area;
END shape;
/From outside, shape.area(2, 3) fails with PLS-00306, because only the one-parameter version is public.
Matching the body to the specification
For every public overload, the body must contain a subprogram with the same name and a matching header. The match is checked for parameter names, modes, datatypes and default presence. If the body has an overload with no matching declaration in the specification, it is treated as a private subprogram. If the specification declares an overload with no body implementation, the package body fails to compile with PLS-00323: subprogram or cursor ... is declared in a package specification and must be defined in the package body.
When you add an overload, you must change the specification. That recompiles the specification and invalidates its dependents, as covered in the chapter on dependencies. Adding an overload can also make existing calls ambiguous. A call such as fmt.to_text(NULL) that worked when to_text had a single declaration starts to fail with PLS-00307 as soon as a second one is added. Check callers that pass NULL literals, untyped values or implicit conversions before you add an overload to a package that other code uses.
Design guidance
- Overload when the operations are conceptually the same and differ only in input type or optional detail. Use separate names when the behavior differs.
- Keep overloads in distinct type families. Do not rely on the compiler ranking
INTEGERagainstNUMBER. - Avoid overloading combined with default parameters unless every call form is clearly unique.
- Have the simpler overloads call the most general one, as
to_text(DATE)does above, so the logic lives in one place. - Write a test block that calls each public overload, including an explicitly typed
NULLcall, so a later change fails in your tests and not in production code.
Exercises
Exercise
A package specification declares PROCEDURE save(pid IN NUMBER) and PROCEDURE save(pid IN NUMBER, p_audit OUT VARCHAR2). Is this a valid overload, and what happens on the call save(10)?
Show answer
It is valid, because the number of parameters differs. The call save(10) supplies one argument, so it matches the first declaration only. The second needs an OUT variable as its second argument, so it does not fit a call with one argument. The call resolves to the first declaration.
Exercise
Why does a package with FUNCTION f(p IN CHAR) RETURN NUMBER and FUNCTION f(p IN VARCHAR2) RETURN NUMBER cause trouble, and how would you fix it?
Show answer
CHAR and VARCHAR2 belong to the same datatype family, so any character argument fits both declarations and the call fails with PLS-00307. Fix it by keeping one function that takes VARCHAR2, or by giving the functions different names if they must behave differently.
Exercise
Using the fmt package from this chapter, why does fmt.to_text(NULL) fail to compile, and what is a correct form of the call for the DATE overload?
Show answer
A bare NULL has no datatype, so both the NUMBER and the one-parameter DATE overloads fit, and the compiler raises PLS-00307. Give it a type, for example fmt.to_text(CAST(NULL AS DATE)).