We've already discussed the main features of a recursive function in Chapter 2, Low-level Programming with C++. Let's take a look at the following simple example of calculating the factorial of a number recursively:
int factorial(int n)
{
if (n <= 1) return 1;
return n * factorial(n - 1);
}
Recursive functions provide elegant solutions compared to their iterative counterparts. However, you should carefully approach the decision to use recursion. One of the most popular issues with recursive functions is stack overflows.