At risk of accidentally solving a different homework problem you have, I will show you an example of recursion very similar to your factorial problem to give you an idea of what you should be going for here.
Consider the problem of exponentiation over non-negative integers. An integer base, b, raised to an integer power, e, can be expressed with a recursive function which takes two variables.
Lets call this function exp, and, because we're only considering integers (particularly, non-negative ones but I don't want to over-complicate this for you) it will return an integer and I'll use ^ to mean a mathematical exponentiation operator, not as any sort of legitimate programming syntax for you to use in your homework.
b^e = b*b*b*b*b*.....*b*b (e times)
This problem appears to have one part which you will use to recur on, and that is the exponent.
This means we have the following prototype for our exp function:
Code:
int exp(int b, int e)
For this problem, the base won't change; we're just passing it in so we know what to multiply by in our recursion. The exponent is what we will
recur on. Our
base case is when e = 0, since for any integer b, b^0 = 1. It is important to identify the part of the problem you will recur on, and your base case for any problem you approach with recursion. Don't confuse the terms
base case and base when I mean base of the exponent. They are completely different.
So how will this function be used? When we want 10^5, for example, we will call exp(10, 5) and expect it to return 100,000 because 10^5 = 100,000.
Now lets get under the hood of this problem. We have our base, b, and our exponent e. Since the exponent is the piece we are recurring on, the first thing we always do is check for the
base case for e = 0 with the following:
Code:
if(e <= 0) return 1;
That is the most important, and easiest, part of the problem. Note that I include negative ints as part of the base case, since we're only concerned with non-negative exponents and for simplicity we'll just treat any negative int as a 0.
Now comes the recursion part.
b^e = b*b*b*b*b*.....*b*b (e times)
Any time we do not have the base case, we will multiply b by the return value of a recursive call to our exp function. Since we start with exponent e, it is logical to recursively call exp with the same b and e - 1 as arguments.
Code:
else return b*exp(b, e - 1);
As a recursive function recurs down to its base case, it accumulates a
stack of function calls. Lets look at the complete exp function:
Code:
int exp(int b, int e)
{
if(e <= 0)
return 1;
else
return b*exp(b, e - 1);
}
Suppose we call exp(2, 3), or in other words, we want to find 2^3.
Call 1:
e is 3, so we do not have the base case
our b is 2, and is what we will multiply by
so we recursively call exp with b, and e-1
return 2*exp(2, 2)
Call 2:
e is 2, so we do not have the base case
exp is recursively called with b, and e - 1
return 2*exp(2, 1)
Call 3:
e is 1, we don't have the base case quite yet
exp is recursively called with b, and e - 1
return 2*exp(2, 0)
Call 4:
e is 0, the base case is met and we return 1 and go back to the return statement in Call 3 ->
Call 3: return 2*1 back to the return statement in Call 2 ->
Call 2: return 2*2 back to the return statement in Call 1 ->
Call 1: return 2*4 = 8
And then finally, the exp finishes computing 2^3 and returns 8.