Return value of function when return path not specified

Jan 8, 2021 at 9:18am
> Could one treat this as guaranteed behavior?
No.

Jan 8, 2021 at 1:04pm
Thinking back decades to when I took my first "C" class, I seem to remember that in the absence of a return statement in a function, the value of the last expression on the stack was returned. In that case, yes, it would be guaranteed behavior.

However, this is fuzzy in my memory, and putting in a return statement is so much better in so many ways, so I never do it without the return statement. I can't be 100% certain that I have my facts straight on this one--but yes, I think the @OP is correct.

But why? Put in the return statement and make it clear what the code is trying to do. Six keystrokes aren't worth the potential headache.
Jan 8, 2021 at 1:13pm
C++ isn't c......
Jan 8, 2021 at 2:30pm
closed account (z05DSL3A)
seeplus wrote:
C++ isn't c......
Yes and no. As the C++ standard has a normative references to a C Standard, C is indispensable for the application of the C++ standard.
Jan 8, 2021 at 3:27pm
The original post has been removed again........... Who's doing all the reporting...........
Jan 9, 2021 at 2:07am
Now the OP is back. It appears the site admin took out a lot of trash.
Jan 9, 2021 at 2:32am
The mad reporter has been slain ... hopefully. :-)
Jan 9, 2021 at 2:51am
Just for completeness sake...
ISO/IEC 9899 is the C standard, as far as I know.
ISO/IEC 9899:TC3 draft section 6.8.6.4, "The return statement", item #12 states,
If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

So there you go. If your foo function reaches }, and you try to use its result, the behavior is undefined. And I'm sure the C++ standard says something along the same lines.

A particular compiler might guarantee behavior that goes beyond that standard, as an extension, but it's still undefined behavior per the standard. But C existed before the ISO standard, and I don't know how things like that worked before standardization. It could be that compilers guaranteed a particular behavior. But in modern C/C++, you certainly cannot assume this.
Last edited on Jan 9, 2021 at 3:02am
Jan 9, 2021 at 4:04am
The difference is:
in C, the undefined behaviour is only if the caller tries to use the result;
in C++, it is undefined behaviour even if the caller ignores the result. https://eel.is/c++draft/stmt.return#4

This difference is understandable; in C++, the return type may be one with a non-trivial destructor.
Jan 9, 2021 at 4:08am
I see, thanks for that info.
Jan 9, 2021 at 7:32pm
No, dutch, they haven't. I am getting blanket reported again.
Jan 9, 2021 at 8:18pm
Oh well, at least we all know how meaningless it is.
Jan 9, 2021 at 9:16pm
Meaningless for established users, horrible for low post count new users who get reported and have their posts deleted.
Jan 9, 2021 at 9:42pm
Looks like the OP has revealed their true colours with the new edit to include the spammy URL.
Jan 10, 2021 at 6:49am
Sayonara to the OP then.
Topic archived. No new replies allowed.