Return from void function

Anirban Sarkar :

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?

paxdiablo :

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Return from void function to another void function

Calling a function from the return statement of a void function

Return default constructed or void from template function

Return Future<void> from a sync function in Dart

GCC : Return statement from a void function in C

How to return a value from Void Function?

Why cannot I return void from a void function

Void Inside of Return function

Return value void * in a function

How to return []byte from internal void * in C function by CGO?

How to return dynamic array from void function in c?

Can I return `void` from a function with a type predicate?

How to convert 'void *' return from a C function to 'UnsafeMutableRawPointer' in Swift 3?

How to access a variable from a void return function in C++

Why is the void implicit return type missing from my Typescript function?

What is the return value if we dont return anything from a non void return typed function in c++?[Experimental]

unexpected non void return value in void function

Function Pointer with void* return and void* parameters

Void function doing return (void)(something)

return statement in void function in C

Restrict function return value to void

returning void* for a function/method return

Bus Error on void function return

Return statement that calls a function that returns void in a function that returns void

Why do I have to return Unit.INSTANCE in a void kotlin function, when called from Java, and not NULL?

Return data from moq void method with action with no parameter to function being tested

C: Does value return from the function take more CPU cycles than void?

A value of type 'Future<bool>' can't be returned from the function because it has a return type of 'Future<void>'

Unexpected non-void return value in void function