Number of recursive function calls

1

Hello. How would you know how many times a function is called recursively by your code?

For example, in the code below, how do you know how many asterisks the function prints for a given value n?

int fibonacci(int n) {
    printf("*");
    if ((n == 1) || (n == 2))
        return (1);
    return (fibonacci(n-1) + fibonacci(n-2));
}
    
asked by anonymous 01.10.2018 / 19:27

1 answer

0

It's easy, you can create a variable to count this:

int qtd = 0;

int fibonacci(int n) {
    qtd += 1;
    printf("chamado: %d \n", qtd);
    if ((n == 1) || (n == 2))
        return (1);
    return (fibonacci(n-1) + fibonacci(n-2));
}

Tested on online_c_compiler

    
01.10.2018 / 19:36