I am using cygwin gcc version 4.5.3 to compile the following code.
/* recursion.c */
int factorial_aux(int n, int t) {
if (n <= 1) {
return t;
} else {
return factorial_aux(n-1, n*t);
}
}
int factorial(int n) {
factorial_aux(n, 1);
}
int main() {
int result = factorial(4);
printf("%d\n", result);
}
Running the program compile with: gcc recursion.c -o recursion.exe prints 24, but the one compile with gcc -O2 recursion.c -o recursion.exe prints 0.
Could someone tell me why the different result? Any possible way to fix it?
Thanks!
Your
factorialfunction is missing something. It doesn’treturnanything.