What is the point of adding a return statement at the end of a void C / C ++ procedure?

I see functions / methods with void return in the signature that have a return statement at the end of the function. What is the reason for this and does this apply to other languages?

For everyone I know, I can use return if I want to exit from a place other than the end of the function.

Example C:

void function(void) { int x = 1 + 2; return; // what do we need this for, if at all? } 
+4
source share
10 answers

It seems pointless. But I suggest that such things can be used to set a breakpoint in the IDE, which do not support setting a breakpoint in the closing bracket and by placing a breakpoint, some values ​​can be checked in the clock window, etc.

+23
source

Someone too literally ran "encoding" for one function. "

+16
source

For a function returning void, this does not matter, since the "fall from the end" of the void function simply returns as you expected.

However, some people like to do this to help with future maintenance. For example, if someone later decides to change the return type of the function to say int, you should get a compiler error if you forget to change this final return statement to return an int value. If you didn’t have a return statement at all, the return value is undefined, and many old compilers did not warn you about this (it's true - I saw it!). This is not a problem these days, as most modern compilers will warn about this.

+7
source

I always include a return statement at the end of my functions, even if they return nothing. My initial reasons for this were that I was explicit, there was no ambiguity as to where the function ended, I was always annoyed by the functions that “left the end”. Subsequently, it was just a habit that was hard to break (if I wanted to).

+5
source

As you yourself indicate, you only need the return statement if you want to exit the function before the last statement. In this example, the return statement has no purpose.

+4
source

This is really pointless ... at least for the compiler, but stylistic, like so many other things in C ++. Example:

 int main () { DoSomething (); return 0; } 

In C ++, returning 0 is completely pointless for the corresponding compiler. However, sometimes people prefer to include redundant or meaningless statements just to be explicit. I would not dare to grind such things to the ignorance of the programmer. This is just a style, do not hesitate to get rid of it if you so wish.

+3
source

This is useless in C / C ++ and simply reduces readability.

One example of a real language, where necessary, is assembler. But in assembler, the return statement is not part of the language. Instead, you need to call a special instruction (ret in x86), which causes the control to return. If you do not, the execution just continues, and what happens next depends on possible factors, luck and the moon.

+3
source

I suppose this is because the function used to return some value, and the programmer probably thought it would be again.

+2
source

I don’t think you need it, but you can get it. Try running the program again and again. See what the difference is.

+2
source

All Articles