# Recursive Functions in R Programming

Last Updated : 05 Jul, 2023

Recursion, in the simplest terms, is a type of looping technique. It exploits the basic working of functions in R

## Recursive Function in R:

Recursion is when the function calls itself. This forms a loop, where every time the function is called, it calls itself again and again and this technique is known as recursion. Since the loops increase the memory we use the recursion. The recursive function uses the concept of recursion to perform iterative tasks they call themselves, again and again, which acts as a loop. These kinds of functions need a stopping condition so that they can stop looping continuously. Recursive functions call themselves. They break down the problem into smaller components. The function() calls itself within the original function() on each of the smaller components. After this, the results will be put together to solve the original problem.

## R

 `rec_fac <- ``function``(x){` `    ``if``(x==0 || x==1)` `    ``{` `        ``return``(1)` `    ``}` `    ``else` `    ``{` `        ``return``(x*``rec_fac``(x-1))` `    ``}` `}`   `rec_fac``(5)`

Output:

`[1] 120`

Here, rec_fac(5) calls rec_fac(4), which then calls rec_fac(3), and so on until the input argument x, has reached 1. The function returns 1 and is destroyed. The return value is multiplied by the argument value and returned. This process continues until the first function call returns its output, giving us the final result.

### Example: Sum of Series Using Recursion

Recursion in R is most useful for finding the sum of self-repeating series. In this example, we will find the sum of squares of a given series of numbers. Sum = 12+22+â€¦+N2

Example:

## R

 `sum_series <- ``function``(vec){` `    ``if``(``length``(vec)<=1)` `    ``{` `        ``return``(vec^2)` `    ``}` `    ``else` `    ``{` `        ``return``(vec[1]^2+``sum_series``(vec[-1]))` `    ``}` `}` `series <- ``c``(1:10)` `sum_series``(series)`

Output:

`[1] 385`

## R

 `sum_n <- ``function``(n) {` `  ``if ``(n == 1) {` `    ``return``(1)` `  ``} ``else` `{` `    ``return``(n + ``sum_n``(n-1))` `  ``}` `}`   `# Test the sum_n function` `sum_n``(5)  `

Output:

`[1] 15`

In this example, the sum_n function recursively increases n until it reaches 1, which is the base case of the recursion, by adding the current value of n to the sum of the first n-1 values.

## R

 `exp_n <- ``function``(base, n) {` `  ``if ``(n == 0) {` `    ``return``(1)` `  ``} ``else` `{` `    ``return``(base * ``exp_n``(base, n-1))` `  ``}` `}`   `# Test the exp_n function` `exp_n``(4, 5)`

Output:

`[1] 1024`

In this example, the base case of the recursion is represented by the exp_n function, which recursively multiplies the base by itself n times until n equals 0.

#### Key Features of R Recursion

• The use of recursion, often, makes the code shorter and it also looks clean.
• It is a simple solution for a few cases.
• It expresses in a function that calls itself.

#### Applications of Recursion in R

• Recursive functions are used in many efficient programming techniques like dynamic programming language(DSL) or divide-and-conquer algorithms.
• In dynamic programming, for both top-down as well as bottom-up approaches, recursion is vital for performance.
• In divide-and-conquer algorithms, we divide a problem into smaller sub-problems that are easier to solve. The output is then built back up to the top. Recursion has a similar process, which is why it is used to implement such algorithms.
• In its essence, recursion is the process of breaking down a problem into many smaller problems, these smaller problems are further broken down until the problem left is trivial. The solution is then built back up piece by piece.

Types of Recursion in R

1. Direct Recursion: The recursion that is direct involves a function calling itself directly. This kind of recursion is the easiest to understand.

2. Indirect Recursion: An indirect recursion is a series of function calls in which one function calls another, which in turn calls the original function.

3. Mutual Recursion: Multiple functions that call each other repeatedly make up mutual recursion. To complete a task, each function depends on the others.

4. Nested Recursion: Nested recursion happens when one recursive function calls another recursively while passing the output of the first call as an argument. The arguments of one recursion are nested inside of this one.

5. Structural Recursion: Recursion that is based on the structure of the data is known as structural recursion. It entails segmenting a complicated data structure into smaller pieces and processing each piece separately.

Previous
Next