I’m implementing some basic data structures in C and I found out that if I omit the return type from a function and call that function the compiler doesn’t generate an error. I compiled with cc file.c and didn’t use -Wall (so I missed the warning) but in other programming languages this is a serious error and the program won’t compile.
Per Graham Borland’s request, here’s a simple example:
int test()
{
printf("Hi!");
}
int main()
{
test();
}
C is an old language and at the time it was introduced, returning integers was common enough to be the default return type of a function. People later started realizing that with more complicated return types, it was best to specify int to be sure you are not forgetting the return type, but in order to maintain backwards compatibility with old code, C could not remove this default behavior. Instead most compilers issue warnings.
If the function reaches the end without a return statement an undefined value is returned except in the
mainfunction, 0 is returned. This has the same reason as above.