Function return optional

Hello,

I was wondering why when compiling the CPP code in Unreal Engine, if a function that needs to return a variable does not return it, the compiler does not compile while in the standard cpp it seams to be optional to return a variable such as int Main() { return 0; } for example.

Thank you!

Hello Philx

With the Main function, the compiler will add a return call if one is not present (defaulting to return 0) as with Main you can only enter and exit that function once and is the entry point for C++ applications.

With functions other than Main, the compiler doesnā€™t have a default option (how could it? it doesnā€™t know what your intention is for that function) so it gives an error during compilation.

I hope this helps, good luck on your learning journey!

5 Likes

Thank you very much for taking time to answer!

@Tuomo_T @Philx I am surprised that

int main()
{ }

has an implicit return 0; in there. Is that part of the language definition or just something that a compiler implementation does to get past a standard situation?

I would have thought that the absence of a return with result of the type specified for the function would be at least a warning everywhere.

Now I am curious to see what happens with other cases, such as a void function in which there is a return.

OK, I get it. A return statement can have no result expression. And for a void-returning function, return; is enough, and there is an impllicit value-less return from any function where operation falls through the function body. The idea of an implicit return 0; for an int main() is apparently a compiler feature. Hereā€™s what Microsoft says about it: return Statement (C) | Microsoft Docs at least as recently as VS 2019.

1 Like

@orcmid I created a new function before the main function to test it with int return and it result with a warning but it compile without any return. Thanks for the link!

I realize Iā€™m late to the party. But for the record adding ā€œreturnā€ to a void function just exits that function. Which is both valid and may be desirable based on what your function does.

Keep in mind that if you do call return in a void function, trying to return a value will cause a compiler error.

1 Like

@Tuomo_T understandably. However, int main(void) is not a void function. It has a specified return type. C99 is pretty strict about matching of returns with the specified return type, requiring no expression if the return type is ā€˜voidā€™. The case at hand is if a function drops through the end of its body without any return. From Harbison & Steele ed.5,

If program control reaches the end of a function body without encountering a return statement, then the effect is as if a return statement with no expression were executed. If the function has a non-void return type, then the behavior is undefined.

That appears to be the gap that allows implementation-dependent implementations of main to imply return 0; although one should not rely on it.

Hereā€™s a consideration that bothers me more. It is possible to invoke a non-void function and do so in a statement that does not employ a returned value. This happens all the time in the case of int-returning functions where the invoking code is essentially ignoring any returned result because it is the side-effect of the operation that is all that matters (i.e., printf and such). This is so commonplace, compilers donā€™t warn about it. Is the same true of other numeric returns, how about non-numeric returns (i.e., returning a struct value)? These are all cases of generally-permissible discarded values. I would expect some sort of warning in some cases, at least as valuable as ones about slack bytes in structs :roll_eyes:

PS: I only have information as recent as C99. I donā€™t know if later revisions of ISO C Language have altered any of this.

1 Like

Privacy & Terms