DEV Community

loading...

Discussion on: Tail Recursion

Collapse
anlutfi profile image
Antonio Lutfi

you could do something like this, to prevent errors when calling fibo:

int fibonacci(int n)
{
    int go(int i, int a, int b)
    {
        if (n == 0)
            return a;
        if (n == 1)
            return b;
        return go(n - 1, b, a + b); 
     }
     return go(n, 0, 1);
}