What is the difference between function and procedure in PL/SQL?

Tony picture Tony · Apr 21, 2009 · Viewed 236.5k times · Source

What is the difference between function and procedure in PL/SQL ?

Answer

Petros picture Petros · Apr 21, 2009

A procedure does not have a return value, whereas a function has.

Example:

CREATE OR REPLACE PROCEDURE my_proc
   (p_name IN VARCHAR2 := 'John') as begin ... end

CREATE OR REPLACE FUNCTION my_func
   (p_name IN VARCHAR2 := 'John') return varchar2 as begin ... end

Notice how the function has a return clause between the parameter list and the "as" keyword. This means that it is expected to have the last statement inside the body of the function read something like:

return(my_varchar2_local_variable);

Where my_varchar2_local_variable is some varchar2 that should be returned by that function.