Yet another productive method: try to "unwind" solution from the END of recursion.
For example, in factorial case:
When the function knows the answer? Of course, if n <= 1 - factorial is equal to 1 by definition! OK, write the first statement:
int factorial(int n)
{
if (n <= 1)
return 1;
// But what else?...
}
Let's continue. Suppose we HAVE a function to calculate factorial for all small numbers < n.
in factorial(int n)
{
if (n < 1)
return 1; // OK, it's our point of rest...
// We are on terra incognita now, but
// let's remember school math...
// Factorial definition helps:
return factorial(n-1) * n;
}
Stop. See how the land lies.
We are trying to call factorial for lesser than n number then more lesser and so on.
We are going to the bottom - of what? Where is this bottom?
Aha, come back to the 1st and 2nd line of the function body. That's a bottom! After that we will rise to the surface, back to starting recursion level, and - hurrah - we know factorial(n-1) - that's all, all done.
It looks like a trick or abstraction: we have no function yet but operate with its call(s) to build its body...
But it's the power of recursive solutions.