I just read in K&R that a function, even if not defined as void, need not return a value. To test I wrote a quick power function and I'm getting reproducible yet unexpected behaviour:
✈demios:cliC$ cat power.c
#include <stdio.h>
int power(int x, int y);
int main() {
printf("%d\n", power(2,5));
return 0;
}//end main
int power (int x, int y){
int i,total=1;
for (i=0;i<y;i++) {
total*=x;
}
}//end power
✈demios:cliC$ cc power.c
✈demios:cliC$ ./a.out
5
✈demios:cliC$
Why is the function returning 5? I would expect 0 or null or NAN or some such thing. Why 5? I know not to rely on this behaviour, and such code would never see production, but I would like to understand it.
Thanks.