Factorial Function Confusing. [message #677690] |
Mon, 07 October 2019 06:12 |
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.
|
|
|