Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If a function returns no value, with a valid return type, is it okay to for the compiler to return garbage?

If a function has a return type other than void, and the function does not return anything, then I guess the compiler returns a garbage value (possibly seen as an uninitialized value). It happens at compile time, so why shouldn't it show an error?

For example,

int func1() {
    return; // error
}

int func2() {
    // does not return anything
}

The second func2 should throw an error, but it does not. Is there a reason for it? My thinking was such that, it can be seen as an uninitialized value, so if we need to throw an error in the second case, then we need to throw error, if an value is uninitialized, say

  int i;  // error
  int i = 6;  // okay

Any thoughts, or is this a duplicate question? I appreciate your help.

like image 317
howtechstuffworks Avatar asked Mar 30 '12 01:03

howtechstuffworks


People also ask

What does a function with no returning value return?

If no return statement appears in a function definition, control automatically returns to the calling function after the last statement of the called function is executed. In this case, the return value of the called function is undefined.

What is the correct return type of a method that returns no value?

Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so.

Should a function contains return statement if it does not return a value?

return statement syntaxA value-returning function should include a return statement, containing an expression. If an expression is not given on a return statement in a function declared with a non- void return type, the compiler issues a warning message.


1 Answers

In C++, such code has undefined behaviour:

[stmt.return]/2 ... 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. ...

Most compilers will produce a warning for code similar to that in the question.

The C++ standard does not require this to be a compile time error because in the general case it would be very difficult to correctly determine whether the code actually runs off the end of the function, or if the function exits through an exception (or a longjmp or similar mechanism).

Consider

int func3() {
    func4();
}

If func4() throws, then this code is totally fine. The compiler might not be able to see the definition of func4() (because of separate compilation), and so cannot know whether it will throw or not.

Furthermore, even if the compiler can prove that func4() does not throw, it would still have to prove that func3() actually gets called before it could legitimately reject the program. Such analysis requires inspection of the entire program, which is incompatible with separate compilation, and which is not even possible in the general case.

like image 139
Mankarse Avatar answered Oct 06 '22 22:10

Mankarse