3.8 RECURSION

Recursion is a programming technique. Here a function is allowed to call itself. It is most suitable in case of algorithms or data structures, which are recursive in nature. For example, binary tree is recursive data structure. If you want to visit all nodes of such a tree, recursion is very useful. However, its use in situations like Fibonacci numbers may keep your computer busy for few hours.

Advantages of recursion

If you see the code for function factorial, there is only one critical line.

number * factorial(number -1);

It does the task of finding the factorial. It means that code written is small and crisp (in most of the cases). Many data structures are recursive in nature. Their handling becomes easy using recursion. The ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.