0

In the section of the code below, there is no return statement in the function but it still compiled and gave the right answer. Is this a normal behavior?

#include <iostream>

int multiply (int x, int y) {
    int product = x * y;
}

int main (int argc, char **argv) {
    std::cout << multiply(4, 5) << std::endl;
    return 0;
 }
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
hello
  • 1,168
  • 4
  • 24
  • 59
  • 3
    Turn all warnings on, or even turn on warnings to be handled as errors. _"Is this a normal behavior?"_ Yes, in sense this is actually _undefined behavior_, it is (Look after your Schrödinger's cat, quickly!). – πάντα ῥεῖ Jul 31 '14 at 00:53
  • _'Why downvote?????'_ Because questions asking about undefined behavior, usually aren't really useful for a broader audience of researchers for _real problems_. – πάντα ῥεῖ Jul 31 '14 at 00:58
  • 1
    So you can simply make a simple comment and walk away??? – hello Jul 31 '14 at 00:58
  • _'... and walk away???'_ Why should I?!? – πάντα ῥεῖ Jul 31 '14 at 00:59
  • Makes no sense. Can you vote down one more time? – hello Jul 31 '14 at 01:00
  • possible duplicate of [Why can you return from a non-void function without returning a value without producing a compiler error?](http://stackoverflow.com/questions/1610030/why-can-you-return-from-a-non-void-function-without-returning-a-value-without-pr) – M.M Jul 31 '14 at 01:25
  • @πάντα ῥεῖ this is a real problem (well - the issue of forgetting to return) – M.M Jul 31 '14 at 01:26
  • I think there may be some miscommunication here. I read the question not as "why does this compile?" (which is answered by Matt's link) but as "why is it returning the correct answer?" If you take that second position, the question is both useful and interesting (and, from what I can tell from a very cursory search, not a dupe). Clarifying. dotman: if that is not the case, please let us know. – paxdiablo Jul 31 '14 at 01:31

1 Answers1

5

No, getting the correct answer from the function is not normal behaviour, it's a coincidence. You should not rely on this.

From C++03, 6.6.3 The return statement /2 (and the same section for C++11 as well):

Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.

You'll probably find that you're getting the correct answer simply because of the side effects of your calling convention.

For example, if a function value is returned in the (mtyhical) r0 register, it may be that the calculation uses that register within the function to hold the value, before storing it into the memory representing the product variable. So the fact it's in r0 when you return is just a hang-over from the way things are done.

Compiling with different levels of optimisation, or using another compiler, or even compiling on a Tuesday evening during a blue moon may affect the outcome, that's the main problem with undefined behaviour - it's untrustworthy. At a bare minimum, a good compiler would have warned you about this.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953