Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Can Sub Procedure/Function Be Called Recursively?

0
10 Posted

Can Sub Procedure/Function Be Called Recursively?

0

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.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.