I know that in order to use a function you must either define it above the main function or at least declare it first. However I noticed that C doesn't throw an error message if my function has a int or void return type
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Answer: %d", cube(3));
return 0;
}
int cube(int num)
{
return num * num * num;
}
I'm still brand new to C could you explain why that rule doesn't affect int return type
Decades ago, function types defaulted to returning an int
. The 1990 C standard said, in clause 6.3.2.2:
… If the expression that precedes the parenthesized argument list in a function call consists solely of an identifier, and if no declaration is visible for this identifier, the identifier is implicitly declared as if, in the innermost block containing the function call, the declaration
extern int identifier();
appeared…
Some C compilers still make provision for this, which is of dubious value in this millennium. You should request the compiler apply a more modern standard, such as by using the Clang switch -std=c17
, as well as enabling warnings with -Wmost
or -Wall
, unless you have occasion to compile code that is more than a third of a century old.