A function returns a value while a stored procedure doesn?t return a value.
One key difference between a function and a stored procedure in PL/SQL is the return type.
In PL/SQL, a function must return a value, while a stored procedure does not have a return type. Functions are designed to return a single value, and that value is specified in the function’s definition. On the other hand, stored procedures are meant to perform an action or a series of actions without necessarily returning a value.
Here’s a brief example to illustrate the difference:
-- Function example
CREATE OR REPLACE FUNCTION calculate_area(radius NUMBER) RETURN NUMBER IS
pi CONSTANT NUMBER := 3.14;
area NUMBER;
BEGIN
area := pi * radius * radius;
RETURN area;
END;
— Procedure exampleCREATE OR REPLACE PROCEDURE print_message(name VARCHAR2) IS
BEGIN
DBMS_OUTPUT.PUT_LINE(‘Hello, ‘ || name || ‘!’);
END;
In the above examples, the calculate_area
function calculates the area of a circle and returns the result, while the print_message
procedure simply prints a greeting message without returning any value.