# R Recursive Function With Examples & Applications

## 1. Objective

In this **R Tutorial**, we are going to cover the most interesting programming feature i.e. R Recursive Function. This tutorial will provide you a detailed description of Recursion. First of all, we will discuss what is the Recursive function, different examples of Recursion in R. Then we will also cover Features and Applications of R Recursive function.

**Get the best books to learn R Programming.**

**2. **What is Recursion?

In a recursive function, function calls itself. In this, to solve the problems we break the programs into smaller and smaller sub-programs.

**For Example:**

4! = 4*3*2*1 = 24

This problem of finding factorial of 4 is been broken down into a sub-problem of multiplying the factorial of 3 with 4

4! = 4*3

Or more generally,

**n! = n*(n-1)!**

Without this, the recursion will not end and continue.

## 3. What is Recursive Function in R?

Recursive functions call themselves. They break down the problem into the smallest possible components. The function() calls itself within the original function() on each of the smaller components. After this, the results will put together to solve the original problem.

Factorial <- function(N) { if (N == 0) return(1) else return( N * Factorial (N-1)) }

## 4. Examples of R Recursive Function

**i. Find Factorial of a number using Recursive function**

recur_factorial <- function(n) { if(n <= 1) { return(1) } else { return(n * recur_factorial(n-1)) } }

**Output:**

> recur_factorial(4)

[1] 24

Here, **recur_factorial() **is used to compute the product up to that number. Let us suppose the user passes 4 to the function. Inside the recur_factorial(), the number 4 is been multiplied to the factorial of (4 – 1 = 3). 3 is been multiplied again to the factorial of (3 – 1 = 2). This goes on until the number reaches 1. Now, all previous functions of 2, 3 and 4 return the result one by one giving you the final result 1 * 2 * 3 * 4 which equals 24.

**ii. Find the sum of natural numbers using the Recursive function.**

calculate_sum() <- function(n) { if(n <= 1) { return(n) } else { return(n + calculate_sum(n-1)) } }

**Output:**

> calculate_sum(4) [1] 10

Here, **calculate_sum(n-1)** is used to compute the addition up to that number. Let;s suppose the user passes 4 to the function. Inside the calculate_sum(n-1) the number added to the sum of (4 – 1 = 3). Now, 4 is added to the sum of (n-1) i.e (4-1) = 3. It gives the final result as 4+(3+2+1) =10

**iii. Find Sum of Series 1²+2²+3²+…..+n² using the Recursive function.**

Sum.Series <- function(number) { if(number == 0) { return (0) } else { return ((number * number ) + Sum.Series(number - 1)) } } Sum.Series(3)

Let us divide the above expression for better understanding

(number * number) = Multiplying the number

Sum.Series(number – 1) = Calling the same function with decremented value (1 number minus)

**Recursion 1**

number = 4 which is Greater than 0 so,

(number * number ) + Sum.Series(number – 1)

(4 * 4) + Sum.Series(4 – 1)

16 + Sum.Series(3)

**Recursion 2**

number will become 3, which is Greater than 0 so,

(number * number ) + Sum.Series(number – 1)

(3 * 3) + Sum.Series(3 – 1)

9 + Sum.Series(2)

Total will be 16 + 9 = 25

**Recursion 3**

number will become 2, which is Greater than 0 so,

(number * number ) + Sum.Series(number – 1)

(2 * 2) + Sum.Series(2 – 1)

4 + Sum.Series(1)

Total will be 16 + 9 + 4 = 29

**Recursion 4**

number will become 1, which is Greater than 0 so,

(number * number ) + Sum.Series(number – 1)

(1 * 1) + Sum.Series(1 – 1)

1 + Sum.Series(0)

Total will be 16 +9 + 4 + 1 = 30

**Recursion 4**

Number will become 0, which means First if condition is True so it will exit from the function.

Final Output will be 30

## 5. Key Features of Recursion

Some Features of Recursive function are:

- The use of recursion, often, makes code shorter and looks clean.
- It is a Simple solution for a few cases.
- It expresses in a function that calls itself.

## 6. Key Applications of Recursion

After learning Recursive Function in R, let’s now discuss the applications of R Recursive Functions.

### i. Dynamic Programming

It is the process to avoid re-computation. It is an essential tool for statistical programming. There are two types of dynamic programming:

**a) Bottom-up dynamic programming**

- In this, we check function starting with smallest possible argument value.
- All computed values will be stored in an array.

**b) Top-down dynamic programming**

- Save each computed value as the final act of a recursive function.
- Check if pre-computed values exist as the first action.

### ii. Divide-And-Conquer Algorithms

- It is a Common class of recursive function.
- Common features of this algorithm are process inputs, divide the input into smaller portions, Recursive call(s) process at least one part.
- Recursion may sometimes occur before the input is been processed.

So, this was all in R Recursive Function. Hope you likeour explanation.

## 7. Conclusion

Hence, We have studied about Recursion, its function, and examples of Recursive function. In this tutorial, we have also learned about Recursive functions which arise often in statistics and dynamic programming along with its type.

Hope you liked this blog, If you have any query about R Recursive function, so you can share with us using the comment box given below.

**See Also-**

Very informative

Wow, that was very useful. Simple, straightforward, yet very informative.