3
#include <stdio>
using std::cout;
void CountDown(int N) {
  if(N == 0) {
    return;
  }
  cout << N;
  CountDown(N-1);  
  //return;
}

In the code the output I get when return is commented is same as when not commented.

What I want to ask is whether it makes a difference if I use a return; statement at the end of the function (since it would implicitly return to the function, which called it, at the end of the braces)?

Another question: What if had a function with a return type instead of void here. I tried it with a function. The value was wrong.But there was no compiler error. So using simply a return; makes no difference right? Except when I want to prematurely end the function right?

  • 1
    It makes no difference whether you put `return;` at the end of a function returning `void`. This has nothing to do with recursion, it is always true. – Jonathan Wakely Sep 02 '15 at 14:17
  • Mmm. In that code, the last `return;` is never reached, isn't it? But in any case, that sentence is no necessary in `void` functions. – Tarod Sep 02 '15 at 14:18
  • 1
    See [what is the purpose of return in the void function](http://stackoverflow.com/q/18638286/1708801) – Shafik Yaghmour Sep 02 '15 at 14:19
  • Re updated question, falling off the end of a value returning function is undefined behavior see [this question](http://stackoverflow.com/q/20614282/1708801) ... because it is undefined behavior no diagnostic is required. – Shafik Yaghmour Sep 02 '15 at 14:34
  • @ShafikYaghmour I didn't get it.What does undefined behaviour mean.And how does it affect the program? I mean its not a compiler error right? – user2546419 Sep 02 '15 at 14:41
  • Undefined behavior is bad, it basically means unpredictable behavior, see the [wikipedia entry](https://en.wikipedia.org/wiki/Undefined_behavior) – Shafik Yaghmour Sep 02 '15 at 14:48
  • If there was no compiler error when you left out the value return your compiler is misbehaving, or you were ignoring its warnings. – molbdnilo Sep 02 '15 at 15:01
  • @molbdnilo Can you check this out please :[link](http://geeksquiz.com/binary-search/).The binary search program in it,first one. If I remove the `return` statements from the recursive function calls to binarySearch again the program gives a wrong answer but there is no compiler error. I am using Codeblocks IDE. – user2546419 Sep 02 '15 at 15:16

2 Answers2

4

For void return functions, no it doesn't make a difference. However, if the function is expected to return anything but void, then you'll need to include a valid return for all code paths.

Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
2

At the end of a void function control is automatically returned to the calling function so you do not need a return; at the end. return in a void function is used to exit the function before it normally would like in your if statement.

If you have a function that is supposed to return a value and does not then that is undefined behavior.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402