The R Programming language introduced new technique called as Recursion for simple, and elegant coding. Recursive functions in R means a function calling itself. To understand the recursive programming, let us consider a well know, yet simple example called factorial.

We can calculate the factorial of any given number using the below formula

n! = (n) * (n-1) * (n-2) * ….. * 1

It means, 6! = 6 * 5 * 4 * 3 * 2 * 1

In R programming, We can achieve the same using For Loop, While Loop etc. But if you observe the above pattern, it’s behavior is repetitive which means recursive. So instead of writing the loops (costly) we can write the recursive functions.

**R CODE**

1 2 3 4 5 6 7 8 9 10 |
# Recursive Functions in R Example Number.factorial <- function(number) { if(number == 0 || number == 1) { return (1) } else { return (number * Number.factorial(number - 1)) } } Sum.Series(6) |

**OUTPUT**

If we pass 0 or 1 as the Number argument value then the function will return 1 else it will return

1 |
(number * Number.factorial(number - 1)) |

Let us calculate for 6!

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
6! = number * Number.factorial(number - 1) = 6 * Number.factorial (6 -1) = 6 * Number.factorial (5) # Recursively calling the above function = 6 * 5 * Number.factorial (5 -1) = 6 * 5 * Number.factorial (4) # Recursively calling the above function = 6 * 5 * 4 * Number.factorial (4 -1) = 6 * 5 * 4 * Number.factorial (3) # Recursively calling the above function = 6 * 5 * 4 * 3 * Number.factorial (3 -1) = 6 * 5 * 4 * 3 * Number.factorial (2) # Recursively calling the above function = 6 * 5 * 4 * 3 * 2 * Number.factorial (2 -1) = 6 * 5 * 4 * 3 * 2 * Number.factorial (1) # Recursively calling the above function = 6 * 5 * 4 * 3 * 2 * 1 = 720 |

In this article we will show you, How to write a simple program using Recursive Functions in R Programming with practical example.

## Use Recursive Functions in R to Find Sum of Series 1²+2²+3²+…..+n²

In this example we will show you, How to write an R program to find the Sum of Series 1²+2²+3²+…..+n² using the Recursive Functions in R Programming

**R CODE**

1 2 3 4 5 6 7 8 9 10 |
# Recursive Functions in R Example Sum.Series <- function(number) { if(number == 0) { return (0) } else { return ((number * number ) + Sum.Series(number - 1)) } } Sum.Series(5) |

**OUTPUT**

**ANALYSIS**

Let me show you the step by step execution process

**Function Definition**

Within the Sum.Series (number) function,

If the user entered Number is 0 then the function will return 0 else it will return

1 |
(number * number ) + Sum.Series(number - 1) |

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)

From the above screenshot you can see that, User entered value is 6:

**Recursion 1**

number = 6 which is Greater than 0 so,

- (number * number ) + Sum.Series(number – 1)
- (6 * 6) + Sum.Series(6 – 1)
- 36 + Sum.Series(5)

**Recursion 2**

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

- (number * number ) + Sum.Series(number – 1)
- (5 * 5) + Sum.Series(5 – 1)
- 25 + Sum.Series(4)

Total will be 36 + 25 = 61

**Recursion 3**

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

- (number * number ) + Sum.Series(number – 1)
- (4 * 4) + Sum.Series(4 – 1)
- 16 + Sum.Series(3)

Total will be 36 + 25 + 16 = 77

**Recursion 4**

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 36 + 25 + 16 + 9 = 86

**Recursion 5**

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 36 + 25 + 16 + 9 + 4 = 90

**Recursion 6**

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 36 + 25 + 16 + 9 + 4 + 1 = 91

**Recursion 7**

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

Final Output will be **91**

**NOTE: **We must use some sort of condition to exit from the recursive calling. If you forgot the condition then the function will execute infinite times (similar to Infinity Loop).

Thank you for Visiting Our Blog