Home >> FAQs/Tutorials >> Oracle Tutorials

Oracle Tutorials - Call Procedure or Function Recursively

By: FYIcenter.com

(Continued from previous topic...)

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 AS              
  2    FUNCTION FACTORIAL(N NUMBER)                             
  3      RETURN NUMBER AS                                       
  4    BEGIN            
  5      IF N <= 1 THEN 
  6        RETURN 1;    
  7      ELSE           
  8        RETURN N*FACTORIAL(N-1);                             
  9      END IF;        
 10    END;             
 11  BEGIN              
 12    DBMS_OUTPUT.PUT_LINE('3! = ' ||
 13      TO_CHAR(FACTORIAL(3)));
 14    DBMS_OUTPUT.PUT_LINE('10! = ' ||
 15      TO_CHAR(FACTORIAL(10)));
 16    DBMS_OUTPUT.PUT_LINE('64! = ' ||
 17      TO_CHAR(FACTORIAL(64)));
 18  END;
 19  /               
                        
SQL> EXECUTE FACTORIAL_TEST;
3! = 6
10! = 3628800
64! = 126886932185884164103433389335161480802000000000000...

There must be something wrong with the FACTORIAL() definition that causes those many extra '0's in the '64!' result.

(Continued on next topic...)

  1. What Is a Procedure?
  2. What Is a Function?
  3. How To Define an Anonymous Procedure without Variables?
  4. How To Define an Anonymous Procedure with Variables?
  5. How To Create a Stored Procedure?
  6. How To Execute a Stored Procedure?
  7. How To Drop a Stored Procedure?
  8. How To Pass Parameters to Procedures?
  9. How To Create a Stored Function?
  10. How To Call a Stored Function?
  11. How To Drop a Stored Function?
  12. How To Call a Stored Function with Parameters?
  13. How To Define a Sub Procedure?
  14. How To Call a Sub Procedure?
  15. How To Define a Sub Function?
  16. Can Sub Procedure/Function Be Called Recursively?
  17. What Happens If Recursive Calls Get Out of Control?
  18. What Is the Order of Defining Local Variables and Sub Procedures/Functions?
  19. What Is the Difference between Formal Parameters and Actual Parameters?
  20. What Are the Parameter Modes Supported by PL/SQL?
  21. How To Use "IN" Parameter Properly?
  22. How To Use "OUT" Parameter Properly?
  23. How To Use "IN OUT" Parameter Properly?
  24. How To Define Default Values for Formal Parameters?
  25. What Are Named Parameters?
  26. What Is the Scope of a Local Variable?

Oracle Tutorials:

More...


Other Tutorials/FAQs:

More...


Related Resources:

More...


Selected Jobs:

More...