The second snippet invokes undefined behaviour.
A function, which reaches the ending }
and the return value is used in the caller, invokes undefined behavior.
Quoting C11
, chapter §6.9.1
If the }
that terminates a function is reached, and the value of the function call is used by
the caller, the behavior is undefined.
So, yes, for a function
- returning anything other than
void
(and whose value is going to be used in caller), must include a return
statement with an expression, which has a
type same as the return type of the function.
- returning
void
, can have a return
statement without any expression. However, this can be also be omitted without any trouble.
Note: Just because "your" compiler (settings) complies the code and produces a binary, does not mean the code is right. Use all the warning flags and strict checks, they will save you lots of trouble.