-4

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?

 } 
FoggyDay
  • 11,962
  • 4
  • 34
  • 48
sommy
  • 31
  • 1
  • 2
    Q: What is the function return [of a function that doesn't return anything]? A: ["undefined behavior"](http://en.wikipedia.org/wiki/Undefined_behavior). A word which here very probably means "random garbage". – FoggyDay Feb 15 '15 at 22:43
  • 2
    There is no return statement, so the behaviour is undefined. It will "return" stuff that was randomly put on the stack (mostly)... or crash.It crashes sometimes, which may suggest that the code works - very nasty bug. – ezaquarii Feb 15 '15 at 22:43
  • That's why higher level languages REQUIRE a return value on all paths inside a function/method. – Leonel Machava Feb 15 '15 at 22:47

2 Answers2

2

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.

M.M
  • 138,810
  • 21
  • 208
  • 365
0

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 and clang 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.

Community
  • 1
  • 1