Doubt in recursive function

6

When I post:

return n * fatqua(n-1) 

The program returns the expected result that is 24 .

But when I post:

return n * fatqua(--1) 

The result of the program is 0 . I can not understand the logic of this operation.

Follow the code:

#include <stdio.h>
#include <stdlib.h>

int fatqua(int n){

    printf("\n numero : %d",n);
    system("Pause");
    if(n>0)
    {
        return n * fatqua(n-1) ;
    }
    return 1;
}

int main(){
    int n, resul=0;

    printf("Digite um numero: ");
    scanf("%d",&n);

    resul= fatqua(n);
    printf("\n\n Fatorial e igual a: %d",resul);

}
    
asked by anonymous 02.11.2016 / 17:17

2 answers

5

Assuming you wanted the parameter --1 :

This causes a compile error:

fat2.c:10:27: error: lvalue required as decrement operand
         return n * fatqua(--1) ;

Just to make it clear, lvalue is the same as locator value . This means that the function expects to receive an addressable value, not a constant.

Assuming you wanted the parameter --n :

Well, here's something interesting and your question has already been asked in stackoverflow. We have this link and in this other some useful answers.

Here are some important points:

  • Using --n will cause the decrease before the statement is executed. Due to the pre-decrement, the evaluated expression will be (n-1) * fatqua(n-1) ;
  • The multiplication is commutative (A * B = B * A), and this makes it dangerous to evaluate this expression. The order of evaluation of the arguments is undefined in this case. This is why it is a good practice not to use a variable that you are incrementing or decrementing twice in the same expression.
  • If you would like to use --n to solve the factorial, run the following code:

    #include <stdio.h>
    unsigned int fatqua(unsigned int n){
        printf("\nnumero : %d", n);
        if (n < 2)
            return 1;
        return fatqua(--n) * (n+1);
    }
    void main() {
        unsigned int n, resul = 0;
        printf("Digite um numero: ");
        scanf("%d", &n);
        resul = fatqua(n);
        printf("\n\n Fatorial de %d eh igual a: %d\n", n, resul);
    }
    

Note that it looks horrible and very less readable than the first form (with (n-1) in the recursive call parameter). Also, one detail would be to use unsigned int also, since there is no factorial of negative number. I stress once again, for the above reasons, that you should use

return n * fatqua(n-1);
    
02.11.2016 / 23:33
1

As already commented, the original intention would probably be to call as - n parameter. Anyway, this is dangerous and spoils the operation, because by operator precedence order in C , the decrement is made before the actual multiplication, generating a recursion of the form

n-1 * n-2 * ... * 2 * 1 * 0 * 1

Where each term is -1 than it should be (note that the last 1 is the base case of recursion, explicit in return 1 ).

On an extra note, it would be even more dangerous to do something like fatqua , because in this case the recursive call occurs before decrementing n. This would generate endless recursions and therefore stack overflow.

    
03.11.2016 / 14:03