|PL/SQL User's Guide and Reference
Part Number A89856-01
PL/SQL Language Elements, 41 of 52
RETURN statement immediately completes the execution of a subprogram and returns control to the caller. Execution then resumes with the statement following the subprogram call. In a function, the
RETURN statement also sets the function identifier to the return value. For more information, see "Using the RETURN Statement".
This is an arbitrarily complex combination of variables, constants, literals, operators, and function calls. The simplest expression consists of a single variable. When the
RETURN statement is executed, the value of
expression is assigned to the function identifier.
Do not confuse the
RETURN statement with the
RETURN clause in a function spec, which specifies the datatype of the return value.
A subprogram can contain several
RETURN statements, none of which need be the last lexical statement. Executing any of them completes the subprogram immediately. However, to have multiple exit points in a subprogram is a poor programming practice.
In procedures, a
RETURN statement cannot contain an expression. The statement simply returns control to the caller before the normal end of the procedure is reached.
However, in functions, a
RETURN statement must contain an expression, which is evaluated when the
RETURN statement is executed. The resulting value is assigned to the function identifier. Therefore, in functions, there must be at least one execution path that leads to a
RETURN statement. Otherwise, PL/SQL raises an exception at run time.
RETURN statement can also be used in an anonymous block to exit the block (and all enclosing blocks) immediately, but the
RETURN statement cannot contain an expression.
In the following example, the function
balance returns the balance of a specified bank account:
FUNCTION balance (acct_id INTEGER) RETURN REAL IS acct_bal REAL; BEGIN SELECT bal INTO acct_bal FROM accts WHERE acctno = acct_id; RETURN acct_bal; END balance;