61

I have to return to the previous level of the recursion. is the syntax like below right?

void f()
{
   // some code here
   //
   return;
}
Dan Olson
  • 22,849
  • 4
  • 42
  • 56
skydoor
  • 25,218
  • 52
  • 147
  • 201
  • 30
    have you tried compiling? – Mitch Wheat Feb 12 '10 at 01:41
  • 36
    @Mitch: Reasonable as that is, it only proves that _your_ compiler supports it, not whether it is actually valid C++. E.g. I can easily "prove" that `void main()` is correct that way. – MSalters Feb 12 '10 at 10:40

9 Answers9

115

Yes, you can return from a void function.

Interestingly, you can also return void from a void function. For example:

void foo()
{
  return void();
}

As expected, this is the same as a plain return;. It may seem esoteric, but the reason is for template consistency:

template<class T>
T default_value()
{
  return T();
}

Here, default_value returns a default-constructed object of type T, and because of the ability to return void, it works even when T = void.

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
  • is it the same thing if in the return instruction, you call a function, which returns a void ? e.g. return f(); where the prototype of f is void f(); – Goaler444 Sep 16 '16 at 14:17
  • 2
    oh wait, so this constructor has void( void ) signature? So `return void( void() )` is valid also. Code obfuscation contest anyone? – Florian Castellane Dec 06 '16 at 13:17
  • 2
    Does this also apply if e.g. I have a `void foo();` and, in `void bar()`, I `return foo()`? – Nic Jun 08 '18 at 19:50
  • oh that's weird. Why does T() need to work for T = void when we never need to instantiate the function template? – codeshot May 30 '19 at 23:52
15

Sure. You just shouldn't be returning an actual value.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
10

Yes, you can use that code to return from the function. (I have to be very verbose here to make Stack Overflow not say that my answer is too short)

icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • 5
    IIRC you used to be able to pad it with spaces :-) – xyz Feb 12 '10 at 01:48
  • You can use ellipses to pad out a few characters, and `(15)` gives you even more while making it clear that you're doing it just for the padding. – Anon. Feb 12 '10 at 01:50
4

Yes, that will return from the function to the previous level of recursion. This is going to be very basic explanation, but when you call a function you are creating a new call stack. In a recursive function you are simply adding to that call stack. By returning from a function, whether you return a value or not, you are moving the stack pointer back to the previous function on the stack. It's sort of like a stack of plates. You keep putting plates on it, but than returning moves the top plate.

You could also verify this by using a debugger. Just put a few break points in your code and step through it. You can verify yourself that it works.

Casey
  • 12,070
  • 18
  • 71
  • 107
  • 2
    Stack frame. New threads get new stacks, function calls get new stack frames on the same stack in a given thread. Just want to clear up that detail because it might confuse newcomers. – stu Jul 11 '16 at 21:00
3

The simple answer to this is YES! C++ recognise void method as a function with no return. It basically tells the compiler that whatever happens, once you see the return; break and leave the method....

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
3

Yes, sometimes you may wish to return void() instead of just nothing.

Consider a void function that wants to call some pass-through void functions without a bunch of if-else.

return 
  InputEvent == E_Pressed ? Controller->Grip() :
  InputEvent == E_Released ? Controller->Release() :
  InputEvent == E_Touched ? Controller->Touch() : void();
ThinkingInBits
  • 10,792
  • 8
  • 57
  • 82
2

You shouldn't have to have the return there, the program will return to the previous function by itself, go into debug mode and step through and you can see it yourself. On the other hand i don't think having a return there will harm the program at all.

Craig
  • 1,199
  • 1
  • 13
  • 27
  • Yes, you are correct. Although without knowing what the some code here reference is referring to it may make sense to be explicit. Although the return just before the closing brace is extraneous. – Casey Feb 12 '10 at 01:45
1

As everyone else said, yes you can. In this example, return is not necessary and questionably serves a purpose. I think what you are referring to is an early return in the middle of a function. You can do that too however it is bad programming practice because it leads to complicated control flow (not single-entry single-exit), along with statements like break. Instead, just skip over the remainder of the function using conditionals like if/else().

dromodel
  • 9,581
  • 12
  • 47
  • 65
0

Yes, for example:

void foo()
{
    // do smth
}

and then

void bar(bool condition)
{
    if (condition) {
        return foo(); //its ok
    }
    // else do smth
}
magrif
  • 396
  • 4
  • 20