l lang="en" op="item"> clang ":19:1: warning: control reaches end of non-void function <-Wretur... | Hacker occupychristmas.org
*
Hacker occupychristmas.org new | past | comments | ask | show | jobs | submit login
:19:1: warning: control reaches end of non-void function <-Wretur..." style="height:10px">



You are watching: Warning: control reaches end of non-void function

petermcneeley on Dec 15, 2018 | parent | favorite | on: C, C++, x86/x64 assembly: The case of forgotten re...
clang ":19:1: warning: control reaches end of non-void function <-Wreturn-type>"Given that no return is undefined behavior (6.6.3 c++ standard) this probably should just be treated as an error. (it would be nice if a manifested return was value/default init but it is not)The reason why these are not errors in the compiler is for backwards compatibility.

Since the article is talking about C/C++In C reaching the end of a non-void function is not undefined behaviour though. It's equivalent to ending with a return; However attempting to use the return value of that function is undefined behaviour. It'd be fine to have that warning be an error when compiling for C++, but not for C.C89 3.6.6.4 (http://port70.net/~nsz/c/c89/c89-draft.html#3.6.6.4):"If a return statement without an expression is executed, and the value of the function call is used by the caller, the behavior is undefined. Reaching the } that terminates a function is equivalent to executing a return statement without an expression."

I’m sure that rule comes from the time when “void” didn’t exist, so functions were defined to return int, but nobody ever bothered to actually return anything.



See more: What Is A Scramble Button Do? What Does A Scramble Button Do

It's definitely there to retain some backwards compatibility with existing K&R C programs. Though it does still somewhat exist in the C11 standards.From 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"Though 6.8.6.4 also says "A return statement without an expression shall only appear in a function whose return type is void"So it seems like a non-void function hitting the end of the block without a return statement is allowed (provided the value isn't used). But having a "return;" in that function would not be in C11.