-1

A very generall question. I work on C, and sometimes when i use recursive algorithms, there are cases when i want to set the "return value" only in "if". And i dont have 'external' return, some compilers dont like it, like in the environment of CodeBlocks it defines it as compiling error, and for example in microsoft visual studio it doesnt. And i dont want to mess with my code, so ill have an external return, because in complicated algorithms it could be difficult, but i know in my case, that it is working for all the exmaples i need.

So, as i understand in educational programms it wouldnt be a problem for most cases (correct me if i wrong - i mean if i know what i do in my algorithm and for which input it works) it souldnt be a problem, but generally, as i understand, if I write a code without 'extern' return, i could have problems, if in some cases, which i didnt thouht about them, it wouldnt return any value or will return junk, am I right ?

Could somebody give me another information about that?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Ilya.K.
  • 291
  • 1
  • 13
  • What do you mean by an 'extern' return? Share code! – CinCout Jun 29 '15 at 12:05
  • Use [code](http://stackoverflow.com/help/mcve) to explain yourself. – Yu Hao Jun 29 '15 at 12:05
  • I mean without if or other conditions. If that not good enough, ill give any examples. – Ilya.K. Jun 29 '15 at 12:08
  • http://stackoverflow.com/questions/1735038/why-not-all-control-paths-return-a-value-is-warning-and-not-an-error – bolov Jun 29 '15 at 12:10
  • btw don't use `extern return`. `extern` has some well defined meanings in C/C++ and that expression is just non-sense to anybody familiar with a C like language. – bolov Jun 29 '15 at 12:17

1 Answers1

0

I assume you mean something like this:

int foo() {
  ...
  if (...) {
     return ...;
  }
  ...
  // should never reach here so no return
}

Failing to return a value from a function that has a non-void return type results in undefined behavior, but is not a semantic error.

Undefined behavior means that anything can happen: the program can crash, the function can (appear to) actually return something.

One way to treat this could be:

int foo() {
  ...
  if (...) {
     return ...;
  }
  ...
  assert(0, "should never reach here");
  return 0;
}

See also Why “not all control paths return a value” is warning and not an error?

Community
  • 1
  • 1
bolov
  • 72,283
  • 15
  • 145
  • 224