2. Function and Procedure
Parameter Types
Named PL/SQL programs (Procedures and Functions) can take
parameters. Parameters are optional on both procedures and
functions.
Keep the parameter name under 30 characters, they must start
with a letter and contain no spaces.
There are 3 types of parameter-
IN, OUT and IN OUT
2
3. Function and Procedure
Parameter Types Cont…
An IN parameter is used as an input only. An IN parameter
cannot be changed by the called program.
An OUT parameter is initially NULL. The program assigns the
parameter a value and that value is returned to the calling
program.
An IN OUT parameter may or may not have an initial value. That
initial value may or may not be modified by the called program.
Any changes made to the parameter are returned to the calling
program.
3
4. Function and Procedure
Parameter Types Cont…
Parameters are declared with data types but without data type
length or precision.
A parameter may be declared as VARCHAR2 but it will not be
declared with a length component (VARCHAR2(30) would not
be valid).
Parameters may also be assigned a default value.
You can either use the assignment operator (:=) or use the
DEFAULT keyword. When a parameter has a default value, you
do not need to include that parameter in the call.
4
5. Sample Parameter Declarations
( parameter_1 IN VARCHAR2 := 'ABC',
parameter_2 IN VARCHAR2 DEFAULT 'ABC',
parameter_3 IN OUT NUMBER,
parameters_can_be_named_anything OUT DATE )
5
6. Procedure Example
CREATE OR REPLACE PROCEDURE my_first_proc (p_name IN
VARCHAR2 := 'Lewis', p_address IN VARCHAR2 := '123
Mockingbird Ln', p_an_in_out_parameter IN OUT NUMBER,
p_an_out_parameter OUT DATE ) IS v_a_variable
VARCHAR2(30);
BEGIN
IF p_name = 'Lewis' THEN
DBMS_OUTPUT.PUT_LINE( p_name || ': ' || p_address );
END IF;
v_a_variable := 99;
p_an_in_out_parameter := v_a_variable; p_an_out_parameter :=
SYSDATE;
END;
6
7. Function Example
CREATE OR REPLACE FUNCTION my_first_func (p_name IN
VARCHAR2 := 'Lewis', p_address IN VARCHAR2 := '123
Mockingbird Ln', p_an_in_out_parameter IN OUT NUMBER,
p_an_out_parameter OUT DATE ) RETURN VARCHAR2 IS
v_a_variable VARCHAR2(30);
BEGIN
IF p_name = 'Lewis' THEN
RETURN -1;
END IF;
v_a_variable := 99;
p_an_in_out_parameter := v_a_variable;
p_an_out_parameter := SYSDATE;
RETURN v_a_variable;
END;
7
8. Procedures VS Functions
Procedures are traditionally the workhorse of the coding world.
Functions are traditionally the smaller, more specific pieces of
code.
In general, if you need to update the chart of accounts, you would
write a procedure.
If you need to retrieve the organization code for a particular GL
account, you would write a function.
A Procedure cannot return a value but a Function MUST return a
value.
8
9. Procedures VS Functions Cont…
The return statement in a function returns control to the calling
program and returns the results of the function.
The return statement of a procedure returns control to the calling
program and cannot return a value.
Functions can be called from SQL, Procedure cannot.
Functions are considered expressions, Procedure are not.
Procedures and functions can both return data in OUT and IN
OUT parameters.
9