Home » SQL & PL/SQL » SQL & PL/SQL » Factorial Function Confusing. (Oracle DB 10.2.0.4.0)
Factorial Function Confusing. [message #677690] Mon, 07 October 2019 06:12 Go to previous message
mamalik
Messages: 268
Registered: November 2008
Location: Pakistan
Senior Member

Dear All,

I saw following code on internet while reading about recursive function. I copied following code and test on my environment. Please see below code.

CREATE OR REPLACE function factorial (
      n positive
    ) return positive
    is
   begin   
      if n = 1 then
        return n;
      else           
     return N * factorial(n-1);
     end if;
end;
/
Then i run following statement and got correct result.
Select Factorial(4) from dual;

Confusion is that in function it is written that "If n = 1 then return n". But when i will call function to calculate factorial of 4, then 4 value is passed and it will be decreased by 1 until its value is 1, when n will be 1 then value will be returned after calculating desired value.

So i think in each case result should be 1, so how is it returning correct value.

Please clear.

Thanks in advance.

Best Regards,
Asif.
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Query Not giving Proper Output and How to write Query In effective way
Next Topic: How to get response from xml in oracle
Goto Forum:
  


Current Time: Sat Apr 20 05:56:22 CDT 2024