Sub topic 6
Forward declarations for private subprograms
This chapter assumes you can write a package specification and body, and that you know the difference between public and private subprograms. You should also be comfortable reading a PL/SQL compiler error. Here you will learn why a private subprogram sometimes needs a forward declaration, how to write one, and which mistakes cause compile errors.
The rule: declare before you use
PL/SQL is compiled in one pass through each declarative part. A name must be declared before any code refers to it. This holds for variables, constants, cursors and subprograms alike.
In a package body, the rule creates two cases:
- Public subprograms are declared in the specification. The compiler reads the specification first, so the body can call them in any order.
- Private subprograms exist only in the body. A private subprogram can be called only by code that appears after its definition.
For most packages the second case causes no trouble. You define helpers first and the public subprograms that use them later. A problem arises in two situations:
- Two private subprograms call each other (mutual recursion). Whichever you write first refers to one that does not exist yet.
- You want the public subprograms at the top of the body for readability, with the helpers below them.
A forward declaration solves both.
What a forward declaration is
A forward declaration is a subprogram header followed by a semicolon, with no IS and no body. It tells the compiler that a subprogram with this name and signature will be defined later in the same declarative part.
FUNCTION is_odd(p_n IN PLS_INTEGER) RETURN BOOLEAN;You write the full definition further down, with the same header followed by IS and a body. From the forward declaration onward, code can call the subprogram, even though its body has not appeared yet.
Mutual recursion
The parity package defines two private-style helpers that call each other. Only is_even is public. is_odd is private, so it is declared in the 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: is_even calls is_odd before is_odd is defined.
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;
/Call it from an anonymous block:
SET SERVEROUTPUT ON
BEGIN
DBMS_OUTPUT.PUT_LINE(CASE WHEN parity.is_even(10) THEN 'even' ELSE 'odd' END);
DBMS_OUTPUT.PUT_LINE(CASE WHEN parity.is_even(7) THEN 'even' ELSE 'odd' END);
END;
/The output is even and then odd. The function is meant for non-negative input. A negative number never reaches zero, so the recursion would run until the session fails with a stack or memory error.
The diagram shows the call chain for parity.is_even(2). The calls alternate between the two functions until p_n reaches zero.
What happens without the forward declaration
If you delete the forward declaration line, is_even refers to a name the compiler has not seen yet. The package body compiles with errors. The message is typically PLS-00313: 'IS_ODD' not declared in this scope, followed by PL/SQL: Statement ignored. In SQL*Plus or SQLcl, run SHOW ERRORS to see them.
Reordering cannot fix this case. Whichever function you place first calls the other, so there is no valid order. Mutual recursion is the one situation where a forward declaration is required.
Ordering for readability
The second reason to use forward declarations is style. Many teams like a body that reads from the top down. The public entry points come first, and the details come later. Here is a package that does this:
CREATE OR REPLACE PACKAGE batch_demo AS
PROCEDURE run(p_items IN PLS_INTEGER);
END batch_demo;
/
CREATE OR REPLACE PACKAGE BODY batch_demo AS
-- Forward declarations for the private helpers defined below.
PROCEDURE log_msg(p_text IN VARCHAR2);
FUNCTION step_label(p_step IN PLS_INTEGER) RETURN VARCHAR2;
PROCEDURE run(p_items IN PLS_INTEGER) IS
BEGIN
FOR i IN 1 .. p_items LOOP
log_msg(step_label(i));
END LOOP;
END run;
PROCEDURE log_msg(p_text IN VARCHAR2) IS
BEGIN
DBMS_OUTPUT.PUT_LINE(TO_CHAR(SYSTIMESTAMP, 'HH24:MI:SS') || ' ' || p_text);
END log_msg;
FUNCTION step_label(p_step IN PLS_INTEGER) RETURN VARCHAR2 IS
BEGIN
RETURN 'Processing item ' || p_step;
END step_label;
END batch_demo;
/Running batch_demo.run(3) prints three timestamped lines. Without the two forward declarations, you would have to move run below its helpers. Both layouts compile and behave the same way. The choice is about how easy the file is to read. The cost is that every private subprogram you want to place late needs a declaration that you must keep in sync with its definition.
Rules the compiler enforces
Forward declarations are checked strictly.
- A definition must follow. If you forward declare a subprogram and never define it, the body fails with
PLS-00328: A subprogram body must be defined for the forward declaration of IS_ODD. - The header must match. The definition must have the same subprogram name, parameter names, parameter order, modes (
IN,OUT,IN OUT), datatypes and return type as the declaration. If they differ, compilation fails. - The same declarative part. The definition must appear in the same declarative part as the forward declaration. In a package body, that means the top level of the body, before the optional initialization section. You cannot declare in the body and define inside another subprogram.
- Subprograms only. A forward declaration does not help with variables, constants or types. Those must still be declared before any code that refers to them.
- Each overload needs its own declaration. If a private subprogram is overloaded, each version that is used before its definition needs a separate forward declaration.
Public subprograms do not need them
Do not forward declare a subprogram that is already in the specification. The specification is the declaration. Code in the body can call a public subprogram before or after its definition.
A related error is the reverse mistake. If you declare a subprogram in the specification and forget to define it in the body, compilation fails. This case reports PLS-00323, which says the subprogram is declared in the package specification and must be defined in the package body.
The table summarizes the cases.
| Situation | Forward declaration needed? |
|---|---|
| Public subprogram called from another body subprogram, any order | No, the specification declares it |
| Private helper defined before the code that calls it | No |
| Private helper defined after the code that calls it | Yes |
| Two private subprograms that call each other | Yes, for at least one of them |
| Private variable or constant used before its declaration | Not applicable, move the declaration up |
For mutual recursion, one forward declaration is enough. The one you declare must be defined after the first caller.
Forward declarations outside packages
The feature belongs to PL/SQL declarative parts in general. You can use it in an anonymous block, in a standalone procedure's declaration section, or inside a nested subprogram. The rules are the same wherever you use it: declare the header, then define the body later in the same declarative part.
Design advice
- Use a forward declaration when it solves a real problem: mutual recursion, or a deliberate and consistent top-down layout.
- If you can fix the problem by moving a definition up a few lines, do that instead. There is then one fewer thing to keep in sync.
- Group forward declarations together at the top of the body, with a comment saying why they exist. A reader who sees a header with no body can then find the real one.
- Keep the private signature small. Fewer parameters means less to keep consistent.
- Consider whether the mutually recursive pair should be one subprogram with a loop or a flag. Recursion that alternates between two functions is often harder to follow than a single function.
Exercise
A package body defines a public procedure report that calls a private procedure format_line. The body defines report first and format_line second. format_line is not in the specification. The body fails to compile. What are two ways to fix it?
Show answer
Option 1: move the definition of format_line above report, so it is declared before it is used. Option 2: add a forward declaration of format_line (its header followed by a semicolon) before report, and keep the full definition where it is. The forward declaration must match the definition's name, parameters and modes exactly.
Exercise
You forward declare FUNCTION is_odd(p_n IN PLS_INTEGER) RETURN BOOLEAN; in a package body. You then define the function as FUNCTION is_odd(p_value IN PLS_INTEGER) RETURN BOOLEAN IS .... What problem does this cause, and how do you fix it?
Show answer
The parameter name differs (p_value instead of p_n), so the definition does not match the forward declaration and the body fails to compile. The fix is to make the two headers identical, using the same parameter name, mode, datatype and return type in both places.