Returning a Void

Can I return in void function?

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.

How to return void in stream?

Stream consists of two mandatory (sourcing, terminal) and one optional (intermediate) parts.

Stream:

  • is generated with sourcing operation (something that creates the Stream<T> instance);
  • is then optionally continued with one or more, chained intermediate operation(s);
  • is finally terminated with terminal operation.

void can only be considered to be the return type of the terminal operation (hence, of its lambda (or method reference) expression) in the stream, because every intermediate operation has to return stream, upon which, subsequent intermediate (or terminal) operation would operate.

For example:

List.of(1, 2, 3, 4)
.stream() //sourcing the stream
.forEach(System.out::println); //terminating the stream

is OK, because println just consumes the stream and doesn't have to return another stream.



List.of(1, 2, 3, 4)
.stream() //sourcing the stream
.filter(System.out::println); //ouch..

however, does not compile.


Additionally, beware, that Stream API is lazy, in Java. Intermediate operations are not effectively evaluated, until the terminal operation is executed.

C++: return the return value of a void function from a void function

From n4582

6.6.3 The return statement [stmt.return]

Paragraph 2

The expr-or-braced-init-list of a return statement is called its operand. A return statement with no operand shall be used only in a function whose return type is cv void, a constructor (12.1), or a destructor (12.4). A return statement with an operand of type void shall be used only in a function whose return type is cv void. A return statement with any other operand shall be used only in a function whose return type is not cv void; the return statement initializes the object or reference to be returned by copy-initialization (8.5) from the operand.

Questions:

But I don't know if it applies to returning void return values.

Yes its perfectly valid to return a void expression from a function that return void. This becomes very handy in templated code so you don't have to special case void functions.

What do you think, does the code comply with the standard?

Yes absolutely.

And would you rather return dummy integers to make the code more readable? Or would the useless return values make the code harder to read?

That's totally up to you and your aesthetics. Does it make the code look more readable to you (or do you have coding guidelines that you need to follow). Personally I would not return dummy values (as the user may expect some meaning from them).

What does the return keyword do in a void method in Java?

It just exits the method at that point. Once return is executed, the rest of the code won't be executed.

eg.

public void test(int n) {
if (n == 1) {
return;
}
else if (n == 2) {
doStuff();
return;
}
doOtherStuff();
}

Note that the compiler is smart enough to tell you some code cannot be reached:

if (n == 3) {
return;
youWillGetAnError(); //compiler error here
}

Returning from a void function

Neither is more correct, so take your pick. The empty return; statement is provided to allow a return in a void function from somewhere other than the end. No other reason I believe.

Why cannot I return void from a void function

There is a way

class A {
Void a() {
// ...
return a();
}
}

but java.lang.Void is an uninstantiable representation of the void type meaning you can't make instances out of it (and, sensibly, you aren't supposed to). Eventually, you would need to return a value, it could be a null - the only "legit" one I can think of.

It has applications with generics and the Reflection API, but I doubt it being used here for the purpose of making a recursive method fancier (?).

class A {
Consumer<String> a() {
return System.out::println;
}
}

You might want to return an instance of a function that returns void. Then, a functional interface java.util.function.Consumer might be a good fit.

Actually, it could be any interface of the kind that would suit you best. For instance,

class A {
Runnable a() {
// ...
return () -> a();
}
}

Is void *function() a pointer to function or a function returning a void*?

The function has the return type void *.

void *function();

So I always prefer in such cases to separate the symbol * from the function name like

void * function();

And as Jarod42 pointed to in a comment you can rewrite the function declaration in C++ using the trailing return type like

auto function() -> void *;

If you want to declare a pointer to function then you should write

void ( *function )();

where the return type is void Or

void * ( *function )();

where the return type void *.

Or a pointer to function that returns pointer to function

void * ( *( *function )() )();

Return void type in C and C++

C11, 6.8.6.4 "The return statement":

A return statement with an expression shall not appear in a function whose return type is void.

No, you may not use an expression, even if it is of void type.

From the foreword of the same document:

Major changes in the second edition included:

[...]

  • return without expression not permitted in function that returns a value (and vice versa)

So this was a change from C89 -> C99 (the second edition of the language standard), and has been that way ever since.


C++14, 6.6.3 "The return statement":

A return statement with an expression of non-void type can be used only in functions returning a value [...]
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.

Yes, you may use an expression if it is of void type (that's been valid since C++98).



Related Topics



Leave a reply



Submit