0

I did a little experimenting while doing some self taught C++ and found that after removing a return statement from a function, executing my compiled code runs the same. Here's a little example code -- if I were to remove the line

return numb;

the code runs just as if it were there

#include <iostream>
using namespace std;
class TheClass {
 public:
  int aNumber(int n) {
    numb = n;
    return numb;                                                                                              
  }
  void printOnScreen() {
    cout << numb << endl;
  }
 private:
  int numb;
};
int main() {
  TheClass exampleObject;
  exampleObject.aNumber(5);
  exampleObject.printOnScreen();
  return 0;
}

Which leads to my question, when is return needed, and when is it not, and also why?

ddavis
  • 337
  • 5
  • 15
  • 2
    It's undefined behaviour to take that out unless it's `main`. – chris May 23 '13 at 06:19
  • you should get some warnings if you don't return numb – billz May 23 '13 at 06:20
  • @chris you should make that an answer (or find one of hte many duplicates :-)) – juanchopanza May 23 '13 at 06:20
  • Which compiler? That code is illegal C++ without the `return` and should give compiler errors. Are you sure you didn't just ignore the error and run the program previously compiled? – Christopher Creutzig May 23 '13 at 06:22
  • @juanchopanza, Precisely, the many duplicates. – chris May 23 '13 at 06:25
  • @ChristopherCreutzig, It's not illegal at all, though I believe MSVC gives errors. I don't remember whether compilers must give a diagnostic, but they will warn if you want them to. Officially, though, it's just undefined behaviour, and it actually hurts when you do it. – chris May 23 '13 at 06:26
  • I'm sure I recompiled -- Apple GCC 4.2.1 – ddavis May 23 '13 at 06:27

2 Answers2

2

If you defined a function as returning a value, a value is always returned if you specify one or not. However, if you omit the return statement, the returned value will be random though because it is whatever happens to be in the register used for returning the value. Depending on how you call the function this might cause no problem, if you ignore the value anyway, but it's bad practice as you can not know if the value is ignored or not. If you don't need to return a value, then declare the function as void.

Devolus
  • 21,661
  • 13
  • 66
  • 113
  • returned value *cannot* be random, because you cannot omit return from function defined as returning value (with one exception, but even then it is not random but 0). – mvp May 28 '13 at 02:07
  • It is random in the sense that it is not predictable. It is whatever happens to be in `EAX` (at least on Intel platforms). On other platforms it is whatever the the desiganted mechanism contains at the time the return is performed. – Devolus May 28 '13 at 07:32
  • Again, returning random data is not possible - your code simply would not compile if return is not present (for non-void function). There is one exception when return can be legally omitted, but even then it is equivalent to `return 0;` - far from random – mvp May 28 '13 at 07:38
  • You should really start getting some idea how compilers work and how data is passed between functions on the machine level. And yes, you can write a function without a return value and you get a warning for it. I have switched on pedantic and wall and I get frequent warnings when develop a prototype. – Devolus May 28 '13 at 08:18
  • Of course, if you force your compiler to shut up and let you omit return statement anyway, junk will be returned - I am not contesting this at all. But, normally, you can not/should not do that. And if you don't do this, returning junk is not possible. – mvp May 28 '13 at 08:23
-1

Return cannot return value and is not needed if your function is declared void.

In all other cases, return value; must be present. One exception is main function when it is declared as int main(...) (typical variations include int main(void), int main(int argc, char** argv)). In this case return may be omitted and reaching end of function execution flow is equivalent to implicit return 0;. Even if this is permitted for main, most compilers still emit a warning about it.

However, if your main function was defined as void main(...) (which is also permitted), this warning will not appear and this is perfectly ok - again because it was declared as void type.

mvp
  • 111,019
  • 13
  • 122
  • 148
  • 2
    Almost right. In C++, main has an implicit return 0 of there is no return, so the many implementations that permit to omit the return are in fact applying the standard. – juanchopanza May 23 '13 at 06:23
  • Thanks, fixed that to `return 0;` – mvp May 23 '13 at 06:24
  • 3
    The point is that it is standard behaviour, so implementations that do not allow that are non-standards compliant. The way you phrased it makes it sound like it is the other way around. – juanchopanza May 23 '13 at 06:25
  • 1
    -1: This answer is misleading. – Jesse Good May 23 '13 at 06:26
  • Ok... except for the fact that `void main` is a disallowed signature :p I can't say I've seen most compilers warn on the implicit return statement for main, though. I haven't put one of those in for a very long time (at least by my standards). – chris May 23 '13 at 06:44
  • 2
    The answer WAS correct before **the question was changed from C to C++ in the title**. Downvoters, please, take a valium! It is not "fair" to let someone to pay for somebody else "unfairness" – Emilio Garavaglia May 23 '13 at 06:47
  • It is true that C++ does not permit `void main(void)`. But C does - gcc 4.6 is perfectly happy with it. – mvp May 23 '13 at 06:51
  • @mvp C doesn't permit that either (ok, technically it could be an "other implementation-defined signature" for the program entry point, but a strictly conforming program can't use one of those) and gcc will complain about it if you turn the warnings on. – zwol May 23 '13 at 07:02
  • @mvp Furthermore, the "implicit `return 0;` you mention *only* happens in C++. C requires an explicit return value for `main`. – zwol May 23 '13 at 07:07
  • 1
    C++ and C99 both have the implicit `return 0;` rule for the `main` function. And both of them disallow the `main` method from returning `void`, except when the implementation explicitly defines such a signature (like in the case of embedded systems). But since everyone's been incorrectly writing `void main` for years, most compilers allow it and only issue a warning when you crank the warning level up to where it should have been to start with. – Cody Gray - on strike May 23 '13 at 07:15
  • @Zack: it works for me in hosted mode in gcc (with warning emitted in pedantic mode). On second issue: I tried it in C and C++ (gcc 4.6), and I could omit return from main without any trouble. – mvp May 23 '13 at 07:15
  • @EmilioGaravaglia to be fair, the early downvotes were for the first version of the answer, which was quite misleading. Furthermore, the question title was changed to C++ way before all this `void main()` business started. – juanchopanza May 23 '13 at 07:41