Why does pre-decrement generate a problem as an argument of a function? Why does Ex1 not work correctly?

Asked

Viewed 45 times

-2

#include <iostream>

using namespace std;

int fatorial(int num);
int main(int argc, char const *argv[]){

cout<< factorial(5);

return 0;
}

int factorial(int num)`{`

if(num > 1)

{

Ex1:

//exit: It makes the factorial of 4, I do not know why of the wrong, and always one less, for example, if in the call of the function were factorial(6), the result would be the factorial of 5, if it were factorial(10) the output would be the result of the factorial of 9

ex1: return num* factorial(--num);

Ex2:

output: 120, this works perfectly.

ex2: return num* factorial(num - 1);

}
    return num;
}
  • 1

    Try to answer: what is the value of num after making --num? And after doing num-1?

  • The same value. I put a Cout<<num<Ndl before the if conditional, and tested both ways. The output was the same, with the exception of the factorial result. The factorial with num-1 the correct result and the factorial with --in a generates an almost certain result, since it gives me the factorial of the predecessor number, if I put in a = 5, gives me the factorial of 4 ie 24, and should have left 120... I do not know if I am explaining right

  • 1

    That’s not what the colleague meant, it’s that --num changes the value of num - and this occurs before it is multiplied by the result of the function.

  • I think I got it

  • So there’s no way, you have to put num -1 even, because if put -in a it will change the value of the variable and if put in a-- of the infinite loop, thank you both very much.

  • This is explained here https://pt.cppreference.com/w/cpp/language/eval_order

Show 1 more comment

1 answer

0

I will try to explain in the way that I understand what is happening, if by chance I make a mistake in something correct me.

In the Ex1 for the multiplication of in a factor * (-num) it is necessary to evaluate first the values of the two operands, even if the precedence of multiplication(*) and pre-decrease(--) is the same, when the evaluation of the second operand is made in a will be decremented affecting the in a of the other operand, causing the multiplication to be made with the value of in a decremented.

For instructions that use more than one expression and the same variable more than once and its value is changed in one of the expressions this can cause undefined behaviors and lock the application, and this should be avoided, in these cases change the value of the variable before or after using it in these instructions.

In the Ex2 the expression num - 1 is evaluated forming a new value that will be copied to the function factorial, in a is not changed in this process, causing the multiplication to be carried out with the correct values.

OBS: In order for you to have executed this Ex1 code you should have the error flags of your compiler disabled, because when I went to test in g++ it did not even compile the code and issued the following error:

error: Operation on ːnum' may be Undefined [-Werror=Sequence-point]

Check and activate your compiler error flags, if you use g++ you can use the following flags:

-pedantic-errors -Wall -Weffc++ -Wextra -Wsign-Conversion -Werror

  • Because it’s true, I hadn’t realized that the --in a is actually num = num-1 and not num - 1, it actually changes the value of the variable. I use vs code to do the code so I may have accidentally disabled it when I was trying to set it to c++ in vs code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.