Ads block

Banner 728x90px

Recursion

Recursion is a process in which a problem is defined in terms of itself. The problem is solved by repeatedly breaking it into smaller problems, which are similar in nature to the original problem.

The smaller problems are solved and their solutions are applied to get the final solution of the original problem.
To implement recursion technique in programming, a function should be capable of calling itself.

A recursion function is a function that calls itself.

main()
{
    ........
    rec();
    ........
} /*End of main()*/
void rec()
{
    ........
    rec();    /*recursive call*/
    ........
} /*End of rec()*/

Here the function rec( ) is calling itself inside its own function body, so rec( ) is a recursive function. When main( ) calls rec( ), the code of rec( ) will be executed and since there is a call to rec( ) inside rec( ), again rec( ) will be executed.


It seems that this process will go on infinitely but in practice, a terminating condition is written inside the recursive function which ends this recursion. The terminating condition is also known as exit condition or the base case.
This is the case when function will stop calling itself and will finally start returning.

Recursion proceeds by repeatedly breaking a problem into smaller versions of the same problem, till finally we get the smallest version of the problem which is simple enough to solve.