Can Sub Procedure/Function Be Called Recursively?
PL/SQL allows sub procedures or functions to be called recursively. The tutorial example below shows you how to calculate factorial values with a recursive sub function:SQL> CREATE OR REPLACE PROCEDURE FACTORIAL_TEST AS2 FUNCTION FACTORIAL(N NUMBER)3 RETURN NUMBER AS4 BEGIN5 IF N <= 1 THEN6 RETURN 1;7 ELSE8 RETURN N*FACTORIAL(N-1);9 END IF;10 END;11 BEGIN12 DBMS_OUTPUT.PUT_LINE(’3! = ‘ || TO_CHAR(FACTORIAL(3)));13 DBMS_OUTPUT.PUT_LINE(’10! = ‘ || TO_CHAR(FACTORIAL(10)));14 DBMS_OUTPUT.PUT_LINE(’64! = ‘ || TO_CHAR(FACTORIAL(64)));15 END;16 /SQL> EXECUTE FACTORIAL_TEST;3! = 610! = 362880064! = 1268869321858841641034333893351614808020000000000000000000…There must be something wrong with the FACTORIAL() definition that causes those many extra ‘0’s in the ‘64!’ result.