Posts Tagged ‘PL/SQL formal parameters’
My students wanted a supplement on how variable modes work in PL/SQL, so I figured it would fit nicely in a quick blog entry. If you’re interested, read on …
PL/SQL supports three patterns of variable modes in functions and procedures. The easiest supports a pass-by-value function or procedure, and it is the
IN mode of operation. The other two are related but different, and support a pass-by-reference function or procedure. The differences between a function and procedure are straightforward: (1) A function can return a value as an output, which is known as an expression; (2) A function can be used as a right operand; (3) A procedure can’t return a value because it more or less returns a void (borrowing from the lexicon of C, C++, C#, or Java and many other languages), (4) A procedure can be used as a statement by itself in a PL/SQL program while a function can’t. The variables you define in a function or procedure signature (or prototype) are the formal paramters, while the values or variables assigned when calling a function or procedure are the actual parameters.
IN mode variable is really a copy of the variable, but you can ask to pass a reference. PL/SQL typically obliges when using the
IN mode of operation. The following defines a pass-by-value PL/SQL function (other than the return type, you could do the same in a procedure too):
You can test the values of the actual parameter before and after the function call while also testing it inside the function. You can also assign a literal number or string as the actual parameter because the
IN mode only requires a value because it discard the variable reference and value when it completes.
There is an exception data type for this IN mode operation, and it is the PL/SQL system reference data type (more on this type can be found in the following post). A PL/SQL reference cursor can only be passed when it is already opened, and it actually passes a reference to the cursor work area in the Private Global Area (PGA).
You can’t assign a value to a formal parameter inside a function when the variable has the default (or
IN) mode of operation. Any attempt to do so raises a
PLS-00363 with a warning that expression (formal parameter) can’t be used as an assignment target. A test of the function follows:
This seems to be the preferred way to implement functions for beginning programmers.
IN OUT mode:
IN OUT mode variable is typically a reference to a copy of the actual variable for a couple reasons. If the function or procedure fails the original values are unchanged (this is a departure from the behavior of collections passed as actual parameters in Java). You can assign values to the formal parameter at runtime when using an
IN OUT mode variable.
At the conclusion of the function or procedure the internal variable’s reference is passed to the calling program scope and replaces the original reference to the actual parameter. Here’s an example of an
IN OUT mode variable in a function.
As you can see the external value is changed inside the function and at completion of the function the external variable passed as an actual parameter is changed:
This seems to be used more frequently in procedures than functions in PL/SQL. However, you can use the approach in either. I’d recommend it for functions that you call through the OCI or Java.
OUT mode variable is very much like an
IN OUT mode variable with one exception. There is no initial value in it. You must assign a value to an
OUT mode variable because it has no value otherwise. If the function or procedure fails, the external variable is unchanged. At the successful conclusion of the function or procedure, the reference for the internal variable replaces the reference to the external scoped variable.
The following shows you the test case:
OUT mode also has an exception, which relates to
BLOB datatypes. You can find more about large objects in this presentation made at the Utah Oracle Users Group – Oracle LOBs.
This should be pretty straightforward but if you have suggestions to improve it let me know.