I am working on some recursion problems, and I have a question about returning a 0 versus a 1. The book shows this function, which I suppose is a factorial function, only when I run it, I get a 0 for the answer. When I changed the return statement from a "return 0" to a "return 1" I got the correct answer - 120. (n = 5)
1 2 3 4 5 6 7
unsigned F(unsigned n)
{
if (n == 0)
return 1;
// else
return n * F(n-1);
}
Why does the book have return 0? Obviously, this is something I should understand by now, but I don't, so I need some guidance. Am I incorrect in assuming it is supposed to return 120? With the above function written with a " return 0" won't 0 always be the answer? So, would you say that what is calculated is the factorial of n, assuming that the return should return 1? Or am I missing something here?