Sub topic 6
Forward Declarations for Private Subprograms
Assume you know basic SQL, PL/SQL variables and control flow, standalone procedures and functions, and basic exception handling. This chapter shows how to make private subprograms call one another when their definitions appear in an order that would otherwise cause a compilation error.
Why a forward declaration is needed
A package body can contain private procedures and functions. These subprograms are declared only in the body, so callers outside the package cannot use them.
PL/SQL normally requires a subprogram to be declared before another subprogram calls it. A forward declaration is a declaration without an implementation. It tells the compiler the subprogram's name, parameters, and return type before its full definition appears.
The syntax ends with a semicolon and does not include IS or AS:
FUNCTION is_odd(p_n IN PLS_INTEGER) RETURN BOOLEAN;You then provide the complete definition later.
Forward declarations are most useful when two private subprograms call each other. This pattern is called mutual recursion. Recursion is when a subprogram calls itself directly or indirectly.
Example: checking even and odd numbers
The package specification exposes only is_even. The helper function is_odd remains private because it appears only in the package body.
CREATE OR REPLACE PACKAGE parity AS
FUNCTION is_even(p_n IN PLS_INTEGER) RETURN BOOLEAN;
END parity;
/
CREATE OR REPLACE PACKAGE BODY parity AS
-- Forward declaration. The full definition appears later.
FUNCTION is_odd(p_n IN PLS_INTEGER) RETURN BOOLEAN;
FUNCTION is_even(p_n IN PLS_INTEGER) RETURN BOOLEAN IS
BEGIN
IF p_n = 0 THEN
RETURN TRUE;
END IF;
RETURN is_odd(p_n - 1);
END is_even;
FUNCTION is_odd(p_n IN PLS_INTEGER) RETURN BOOLEAN IS
BEGIN
IF p_n = 0 THEN
RETURN FALSE;
END IF;
RETURN is_even(p_n - 1);
END is_odd;
END parity;
/The forward declaration allows is_even to call is_odd even though the full definition of is_odd comes later. The package body already knows the signature of is_odd.
The recursion stops when p_n reaches zero. An even number returns TRUE at that point. An odd number returns FALSE. Each call reduces the number by one and switches to the other function.
You can call the public function from an anonymous block:
SET SERVEROUTPUT ON
BEGIN
IF parity.is_even(8) THEN
DBMS_OUTPUT.PUT_LINE('8 is even');
ELSE
DBMS_OUTPUT.PUT_LINE('8 is odd');
END IF;
END;
/The private function cannot be called directly:
BEGIN
-- This fails because IS_ODD is not in the package specification.
IF parity.is_odd(7) THEN
DBMS_OUTPUT.PUT_LINE('7 is odd');
END IF;
END;
/Declaration order and scope
A forward declaration affects only the current declarative region. In this example, both functions are declared in the package body, so the declaration makes is_odd available to later code in that body.
The later definition must match the forward declaration. Its name, parameter modes, parameter types, and return type must be compatible. If they do not match, the package body fails to compile.
A forward declaration does not make a private subprogram public. Public visibility is controlled by the package specification. Anything declared only in the body remains available only to code within the body.
How the compiler resolves the calls
The compiler reads the forward declaration first. It can then compile is_even. When it reaches the full definition of is_odd, it verifies that the definition supplies the promised implementation.
Common errors
- Omitting the semicolon from the forward declaration.
- Writing
ISin the forward declaration. - Changing the parameter list between the declaration and definition.
- Declaring a helper in the specification by mistake, which makes it public.
- Writing recursion without a stopping condition, which can cause excessive calls or an error.
Exercise
In the parity package, what would happen if the forward declaration of isodd were removed while iseven remained above the full definition of is_odd?
Show answer
The package body would fail to compile because iseven references isodd before the compiler has seen its declaration.