Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return from void function

Suppose a class exists as follows:

class Foo
{
    void do_after_something()
    {
    //some code here
    return;
    }

    void do_something()
    {
    //some code here
    return do_after_something(); //returning another (void) function
    }
};

JAVA is clearly opposed to something like the above, Borland C++ compiler issues a warning, MS VC++ does not complain.

My question is: Should returning from a void function be logically (theoretically) correct?
return do_after_something();

as opposed to:

do_after_something();
return;

or is it all implementation (compiler/language) dependent?

like image 406
Anirban Sarkar Avatar asked Nov 26 '14 06:11

Anirban Sarkar


3 Answers

Philosophically, you could argue that returning the result of a void-returning function should be allowed but, sadly, that's not the case here, at least for Java.

It is valid for C++ however. If you try out the following program:

#include <iostream>

void xyzzy(void) {}
void plugh(void) { return xyzzy();}

int main() {
    std::cout << "Hello\n";
    plugh();
    return 0;
}

it will work fine.

This is detailed in ISO C++11 6.6.3 /3:

A return statement with an expression of type void can be used only in functions with a return type of cv void; the expression is evaluated just before the function returns to its caller.

So it's really equally valid to argue that the Java way is correct if you think of void as not an actual type, but as an absence of something. For example, when you have:

int xyzzy(void) { return 42; }

in C, you're not forced to provide an argument of the correct (non-)type, such as with:

void plugh;
int twisty = xyzzy(plugh);

Ditto, the C++ way is correct as well, but in a different way - the languages are what they are.

like image 199
paxdiablo Avatar answered Nov 03 '22 04:11

paxdiablo


I think it should be allowed and considered valid in all languages. If you have a function whose return type is void and you return the result of an expression whose type is void (such as calling another void function), you've satisfied that constraint.

It's not considered useful to do this in C (though I think it may be allowed) because there's no reason to need it. Anytime you do:

return someVoidFn();

You can always translate that to:

someVoidFn();
return;

And get the exact same effect.

However, in C++ and Java, returning a void function does have a real use, and that's because those languages have templates and generics. Consider this (not very useful) Java class:

class NestedIdentity<T> {
  T run(int i, T value) {
    if (i == 0) return value;
    return run(i - 1);
  }
}

Its run method returns the value you pass to it, after calling itself a given number of times. I know, pointless, right? But the important part is that T may be void. You can do this:

NestedIdentity<Void> nest = new NestedIdentity<Void>();
nest(5, null);

And this works. In Java, Void (note the capitalization) instantiates a generic with a void-like type whose only value is null. If Java didn't allow returning a void expression in a void method, it would have to be a compile-time error to instantiate a generic with void.

like image 36
munificent Avatar answered Nov 03 '22 02:11

munificent


In C++ 11 its possible and legal . You can return a void function from other void function. Refrence The C++ Programming Language Chapter 12 , Bjarne Strousstrup

like image 1
Ali Kazmi Avatar answered Nov 03 '22 03:11

Ali Kazmi