Originally Posted by

**Deshe**
I've running out of ideas...

Any help?

Here's a bit more explanation about what Prelude wrote.

Take (for an example) something simple: a function that takes two arguments, N and M, and computes M * N! (that's N factorial). You could create this with a while loop. I'll use a goto statement, just for fun. (It's just an example, not code that I'd want to share with anybody else.)

Code:

int factorial_multiply(int M, int N) {
top_of_function:
if (N == 0) {
return M;
}
M = M * N;
N = N - 1;
goto top_of_function;
}

The algorithm is simple: it multiplies M by all the numbers from 1 to N. I would divide this algorithm into two logical parts: The first part tests if N is zero, and if that's the case, returns M. The second part updates the variables M and N with new values, and then jumps to the top of the function.

Here's an alternative (but very similar) version of the algorithm:

Code:

int factorial_multiply(int N, int M) {
if (N == 0) {
return M;
}
return factorial_multiply(M * N, N - 1);
}

It could be described the same way as before: "The first part tests if N is zero, and if that's the case, returns M. The second part updates the variables M and N with new values, and then jumps to the top of the function."

Any time you have a recursive call whose return value gets returned immediately, you can think of the recursive call as a "goto with arguments." The line 'return factorial_multiply(M * N, N - 1);' effectively picks new values for N and M, and then jumps back to the top of the function.