what is the function return in term of argument n? please hep me to understand?
unsigned int f(unisigned int n) // function declaration
{
// wht f function return in term of argument n?
}
what is the function return in term of argument n? please hep me to understand?
unsigned int f(unisigned int n) // function declaration
{
// wht f function return in term of argument n?
}
If the calling code attempted to access the return value then it would be undefined behaviour:
However if the return value is never used then it is well-defined and nothing special happens. The compiler is not required to give a warning.
int main()
{
foo(6); // OK
unsigned int foo = f(5); // UB
}
It is a terrible idea to write code like this on purpose, of course.
Although this question isn't an exact duplicate of Why does this C++ snippet compile (non-void function does not return a value), it contains an excellent answer by Shafik Yaghmour:
This is undefined behavior from the C++11 draft standard section
6.6.3
The return statement paragraph 2 which says:[...] Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function. [...]
This means that the compiler is not obligated provide an error nor a warning usually because it can be difficult to diagnose in all cases. We can see this from the definition of undefined behavior in the draft standard in section
1.3.24
which says:[...]Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).[...]
Although in this case we can get both
gcc
andclang
to generate a warning using the-Wall
flag, which gives me a warning similar to this:warning: control reaches end of non-void function [-Wreturn-type]
In theory, anything can arise from undefined behavior. In practice, you're likely to get a garbage result.