So I'm just starting to learn about recursion and I'm confused as to how avoid this problem. I will write a function that needs to store a variable, like a sum, but I have to declare the variable in the function so every time the recursive call is made the variable gets initialized again. How can I fix this? I think a helper function of some short but I'm not really sure.
int fib_tail(int n)
{
int fibResult = 0;
int x = 0; // used for bottom-up approach
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
if (n > 1 && x <= n) {
fibResult += fib(x);
}
++x;
return fib_tail(n);
}