Recursion is a programming technique where a function calls itself again and again.
Base case: Every recursive function uses a condition to terminate. It is known as base case. As soon as the condition for base case is true the control terminates the function otherwise, it keeps calling the function again and again(infinitely).
The statement that calls the function repeatedly from its block is said to be ‘Recursive case’.

Advantages of recursion
* It makes the code small and clear so the program code becomes clear and easy to understand.
* Minimum number of variables are used
* The algorithm is precise, readable and efficient
*Easier to debug and obtain error free result.

Limitations of recursion
* More execution time
* It can result in stack overflow
* Occupies more space
Example code of a recursive function:

Types of recursion
1. Direct recursion (linear recursion)
In this type of recursion the function is called from within the same block.
Two categories of direct recursion are
* Tail recursion: In this type of recursion recursive call is the last statement of the function. For example factorial of a number code.

//Recursive factorial function 
int fact(int n)
{
if(n==0) return 1;
else 
return (n* fact(n-1));
}

* Binary recursion:In this type of recursion function calls itself from two distinct points. For example GCD program code.

//Recursive GCD function
int gcd(int n1,int n2)
{
if(n1==n2)
return n1;
else
if(n1>n2) 
return(gcd(n1-n2,n2));
else
return(gcd(n1,n2-n1));
}

2. Indirect Recursion (Mutual Recursion):
In this type of recursion the function is called from another function block mutually. Let two functions A and B such that function B is called from function A and vice versa then this technique is called mutual recursion.
For programs related to recursion click the given link
https://java4school.com/armstrong-number-with-recursion
https://java4school.com/binary-search

LEAVE A REPLY

Please enter your comment!
Please enter your name here