Chapter 14. Procedures, Functions, and Blocks

Beginner

Q:

14-1.

A procedure executes one or more statements and then terminates. A function executes one or more statements and then returns a result via the RETURN statement.

Q:

14-2.

Procedures and functions are composed of the following four sections:

Section

Description

Optional or Required?

Header

The “signature” of the program: its name, parameter list (if any), and RETURN clause if a function

Required

Declaration

A series of declarations of data structures and TYPEs that exist only for the duration of the execution of the program

Optional; you do not have to declare any local data elements

Execution

One or more executable statements performed by the program

Required; both procedures and functions must have at least one executable statement to be valid

Exception

One or more exception handler clauses that trap and handle errors that occur in the executable section

Optional

Q:

14-3.

Use the RETURN statement to return a value from a function. It has the following format:

RETURN expression;

where expression is a literal, variable, or complex expression whose datatype matches (or can be converted to) the datatype in the function’s header’s RETURN clause.

You can use RETURN inside a procedure, but you may not specify data to be returned. Instead you simply state:

RETURN;

The procedure immediately terminates and returns control to the enclosing block. You should avoid using RETURN in a procedure, however, because it leads to unstructured code that is hard to read ...

Get Oracle PL/SQL Programming: A Developer's Workbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.