long long int fact( long long int in ){
return in < 2 ? : in * fact(in--);
}
Suffers from the following problems.
Problem 1
You don't have a value in the "true" part of the ternary operator.
return in < 2 ? : in * fact(in--);
^^ Missing value
Problem 2
Depending on whether in
or fact(in--)
is evaluated first, the function could be:
long long int fact( long long int in ){
int temp1 = in;
in--
long long temp2 = in < 2 ? : (temp1-1) * fact(temp1);
return temp2;
}
or
long long int fact( long long int in ){
int temp1 = in;
in--;
long long temp2 = in < 2 ? : temp1 * fact(temp1);
return temp2;
}
Problem 3
Regardless of how the term in * fact(in--)
is evaluated, the value passed to the next invocation of the function is in
, not in - 1
. Hence, you run into infinite recursion, which causes stack overflow.
Solution
long long int fact( long long int in ){
return in < 2 ? 1 : in * fact(in-1);
}