## DEV Community

Andrew Garcia, PhD

Posted on • Updated on

# Recursion

When reduced to a concise example, recursion can be quite a simple concept to grasp.

The below recursive function:

$f(x,\mathrm{count}) = 10 + x + f(x,\mathrm{count - 1})$

Takes a summand $x$ float and a $\mathrm{count}$ index which defines the number of recursions. The function decreases this index with every recursion, and terminates the function with a "0" when the $\mathrm{count}$ value becomes 0.
Defining this function in code:

#include <bits/stdc++.h>
using namespace std;

{
if (count == 0){return 0;} // termination
return 10 + x + recurAdd(x, count - 1);
}

// Driver code
int main()
{
return 0;
}


This small program will output 31.5 as a final value. We can represent the complete recursion in the following way:

$f_1 = 10 + 0.5 + ( 10 + 0.5 + ( 10 + 0.5 (0) ) )$

Where the recursion can be broken down in the following way, from its first to last input iteration:

$f_1 = 10 + 0.5 + f_2 \\ f_2 = 10 + 0.5 + f_3 \\ f_3 = 10 + 0.5 + f_4 \\ f_4 = 0$

After the last input function $f_4$ has been terminated, the running of operations can be visualized to occur from the deepest $f_4$ to the outermost $f_1$ nest. I visualize this process with the following schematic:

Though not all recursion algorithms may follow this pattern, this visual has helped me simplify my understanding of recursion. I hope my visual helps you too.

-Andrew