Sunday, March 2, 2014

Recursion

  When talking about recursion in programming (Python, Java...etc.), the first problem raising up in my head is " what is recursion" ? From my understanding, recursion is a method that is defined within its own definition. In other word, recursion uses itself to operate as a function. So, the next question is why is recursion so powerful? Or why is recursion necessary in programming? According to Wikipedia - The power of recursion evidently lies in the possibility of defining an infinite set of objects by a finite statement. In the same manner, an infinite number of computations can be described by a finite recursive program, even if this program contains no explicit repetitions.If it was still hard to understand, I will show a very simple example of recursion. void myMethod( int counter)
{
if(counter == 0)
     return;
else
       {
       System.out.println(""+counter);
       myMethod(--counter);
       return;
       }
}
(source - http://danzig.jct.ac.il/java_class/recursion.html)

As we can see, this recursion is not infinite, it is essentially a loop. When should we use recursion to solve problems? We use recursion when we can see that our problem can be reduced to a simpler function to be solved.
  Now lets talk about the characteristics of recursion:
  1. Every recursion function should have a simple base case that is the final reduced solution, and of course a       return value.
  2. A very sophisticated problem set that is intended for recursion to happen.
  3. A recursive call which passes the simpler problem back into the method.
Now we have discuss about recursive functions, I came to a better understanding of recursion. Throughout these weeks, I learned how to solve recursive programming, how it works, and how to make such functions.
I will continue to practice recursion, because I know it is very important in not only python, but it is also very important in other  programming languages.

No comments:

Post a Comment