Procedure and Function Parameters v11
An important aspect of using procedures and functions is the capability to pass data from the calling program to the procedure or function and to receive data back from the procedure or function. This is accomplished by using parameters.
Parameters are declared in the procedure or function definition, enclosed within parenthesis following the procedure or function name. Parameters declared in the procedure or function definition are known as formal parameters. When the procedure or function is invoked, the calling program supplies the actual data that is to be used in the called program’s processing as well as the variables that are to receive the results of the called program’s processing. The data and variables supplied by the calling program when the procedure or function is called are referred to as the actual parameters.
The following is the general format of a formal parameter declaration.
name
is an identifier assigned to the formal parameter. If specified, IN
defines the parameter for receiving input data into the procedure or function. An IN
parameter can also be initialized to a default value. If specified, OUT
defines the parameter for returning data from the procedure or function. If specified, IN OUT
allows the parameter to be used for both input and output. If all of IN, OUT
, and IN OUT
are omitted, then the parameter acts as if it were defined as IN
by default. Whether a parameter is IN, OUT
, or IN OUT
is referred to as the parameter’s mode. data_type
defines the data type of the parameter. value
is a default value assigned to an IN
parameter in the called program if an actual parameter is not specified in the call.
The following is an example of a procedure that takes parameters:
In this example, p_deptno
is an IN
formal parameter, p_empno
and p_ename
are IN OUT
formal parameters, and p_job, p_hiredate
, and p_sal
are OUT
formal parameters.
Note
In the previous example, no maximum length was specified on the VARCHAR2
parameters and no precision and scale were specified on the NUMBER
parameters. It is illegal to specify a length, precision, scale or other constraints on parameter declarations. These constraints are automatically inherited from the actual parameters that are used when the procedure or function is called.
The emp_query
procedure can be called by another program, passing it the actual parameters. The following is an example of another SPL program that calls emp_query
.
In this example, v_deptno, v_empno, v_ename, v_job, v_hiredate
, and v_sal
are the actual parameters.
The output from the preceding example is shown as follows:
positional_vs_named_parameter_notation parameter_modes using_default_values_in_parameters