Yes. When you apply staticto a function, it is not the same as a static variable in a recursive function (which is the problem).
The first simply controls whether the function will be visible outside the compilation unit (for example, for the linker).
The latter means that there is only one copy of the variable for all levels of recursion, and not one at the recursion level, which is usually necessary.
So:
static unsigned int fact (unsigned int n) {
if (n == 1U) return 1;
return n * fact (n-1);
}
ok but:
static unsigned int fact (unsigned int n) {
static unsigned int local_n;
local_n = n;
if (local_n == 1U) return 1;
return local_n * fact (local_n-1);
}
no, as the static variable will be corrupted.
source
share