PL/SQL Recursive Function
In PL/SQL a recursive FUNCTION
is a function which either calls itself or is in a potential cycle of function calls. As the definition specifies, there are two types of recursive functions. When a subprogram calls itself, it is referred to as a recursive call and the process is known as recursion.
PL/SQL Recursive Function example
The following PL/SQL show how to calculate the factorial of a number using Recursive
FUNCTION
:
Example
DECLARE
num number;
factorial number;
FUNCTION calculateFact(x number)
RETURN number
IS
f number;
BEGIN
IF x = 0 THEN
f := 1;
ELSE
f := x * calculateFact(x-1);
END IF;
RETURN f;
END;
BEGIN
num:= 6;
factorial := calculateFact(num);
DBMS_OUTPUT.PUT_LINE(' Factorial '|| num || ' is ' || factorial);
END;