<?php
function factorial_of_a($n)
{
if($n ==0)
{
return 1;
}
else
{
return $n * factorial_of_a( $n - 1 );
}
}
print_r( factorial_of_a(5) );
?>
My doubt is:
return $n * factorial_of_a( $n - 1 ) ;
In this statement - it gives a result of 20 when $n = 5
and $n - 1 = 4
. But how come the answer 120 when I run it? Well, 120
is the right answer... I don't understand how it works. I used for
-loop instead and it was working fine.