Recursion help please

<p>Hello</p>

<p>I am in computer science A AP and we started recursion recently. Can someone please explain the concept in a way other than saying it is a method that repeats itself. I have tried to look at Java help sites, but the explanations there are too abstract for me. Any help would be greatly appreciated.</p>

<p>A recursive method repeatedly calls itself. This is not iterative; each time it calls itself it must come back through the stack.</p>

<p>A simple example is a factorial function. 10! is the same this as 10 * 9!. So if you could construct a function like this (without proper syntax):</p>

<p>factorial (int n){
if (n != 0)
return n * factorial (n-1);
}</p>

<p>So if you put in factorial(10) it would go on to do 10 * factorial(9) then 10 * 9 * factorial(8) and so on until it gets to 10 * 9 * 8...*2 * 1 * factorial(0). It would not call itself any further because of the if statement and would go back through the stack - this is actually when it computes the final result.</p>

<p>The best way to learn this is through looking at examples. Other examples of recursion include the Fibonacci numbers and the Towers of Hanoi problem.</p>