Im trying to write a recursive function which returns the factorial of a number. If the number is 0 or negative then it should return 0.
However everytime i test it, it always returns 0, can anyone shed some light on this please?
int factorial( int integer)
{
if( integer <= 0)
{
return 0;
}
else
return integer* (factorial(integer-1));
}
As it keeps calling itself with integer – 1, it will eventually call itself with 0, and then you will have a call like this:
which will resolve to
which is 0