Recursive functions : Recursive Functions « Function Procedure Packages « Oracle PL/SQL Tutorial






SQL>
SQL> set serveroutput on
SQL> set echo on
SQL>
SQL> CREATE OR REPLACE FUNCTION Factorial(p_MyNum INTEGER) RETURN NUMBER AS
  2  BEGIN
  3       IF p_MyNum = 1 THEN -- Checking for last value to process of n-1
  4            RETURN 1;
  5       ELSE
  6            RETURN(p_MyNum * Factorial(p_MyNum-1)); -- Recursive
  7       END IF;
  8  END;
  9  /

Function created.

SQL>
SQL> BEGIN
  2       DBMS_OUTPUT.PUT_LINE(factorial(10));
  3  END;
  4  /
3628800

PL/SQL procedure successfully completed.








27.4.Recursive Functions
27.4.1.Recursion
27.4.2.Recursive functions