Recursion is the self-invocation of a function. If a function calls itself, it is a recursive function. Every function that uses iteration can be rewritten by using recursion. However there are few problems that are well suited for recursion, viz., factorial, Akermann function, binary search, quick sort, towers of hanoi, and so on. Use of recursion reduces the code size. But, to understand it, one needs to draw a tree depicting the order of function calls and traverse it in depth first fashion. There are several types of recursion: linear recursion, non-linear recursion and tail recursion. A fascinating concept indeed!!
|