Recursive Functions in R

Recursive Function is the type of function which calls itself.It is a looping technique. This forms a loop where every time the function is called. It can call the function itself again and again. The function which can call itself again and again is called recursion. This kind of function requires a stopping condition so that they can stop looping continuously.

The function() calls itself within the original function() on each of the smaller components. It is used for elegant and straight forward coding. The recursion make the coder look shorter and cleaner. 

Characteristics

  1. It is a type of function which call itself.
  2. Its speed is slow.
  3. It must have a termination condition or stopping condition and recursive expression.

Advantage

  1. Recursion makes the code cleaner.
  2. It shorten the complex and nested codes.

 Disadvantage

  1. It is hard to understand the logic behind the recursion function
  2. It is tough to debug recursion function.

Application

  1. It is used in dynamic programming language.
  2. It is also used in divided and conquer algorithm.
  3. We can divide the problem into sub-problem or that makes it easier to solve. Recursion has a similar process, which is why it is used to implement such algorithms.The solution is then built back up piece by piece.

Example

recursion_function<-function(x)
{
    if(x==0 || x==1)
    {
        return(1)
    }   
    else
    {
        return(x*recursion_function(x-1))
    }
 }
 
 recursion_function(6)

Output: [1] 720