Recursion occurs when a function calls itself. For example, consider two implementations of the factorial function:
?inline
int f(int n) {
int res = 1, i = 1;
while (i++ < n)
res *= i;
return res;
}As seen here, mutable local variables can help you avoid recursion.
?inline
int f(int n) {
if (n == 0)
return 1;
else
return n * f(n-1);
}if (n == 0) here is known as a base case,
at least one of which is necessary to prevent infinite recursion.
<:stackoverflow:874353689031233606>
Is recursion ever faster than looping?
•
learncpp.com - Recursion
•
Wikipedia - Tail Call (important for optimizations)
?creditFooter 614056212803092480