I am supposed to write a reentrant factorial function, when I searched what a reentrant function is, I found many definitions, such as a reentrant function shouldn't use static or global variable,and the function cannot be changed while in use , I avoided using static or global variables, but I don't know if it is enough for my function be be reentrant or not,
#include <stdio.h>
int fact(int n){
int c,fact = 1;
for (c = 1; c <= n; c++)
fact = fact * c;
return fact;
}
int main()
{
int n;
printf("Enter a number to calculate its factorial\n");
scanf("%d", &n);
fact(n);
printf("Factorial of %d = %d\n", n, fact(n));
return 0;
}
As written, your function is not just reentrant, it is also pure (in the terminology of some compilers, __attribute__((const))).
The reason is that:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With