Is Returning Void Valid Code

Is returning void valid code?

It's a language feature of C++

C++ (ISO 14882:2003) 6.6.3/3

A return statement with an expression of type “cv 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.

C (ISO 9899:1999) 6.8.6.4/1

A return statement with an expression shall not appear in a function whose return type
is 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.

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).

Is 'void' a valid return value for a function?

void is not an actual return (data)type! void says there is no result. So you can not return a value in a method that's declared void even though the method you're calling is also declared void.

I must admit it would be a nice shortcut, but it's not how things work :-)


Just an additional thought: If what you want was allowed, void would become both a data type and also the only possible value of that data type, as return x; is defined as returning the value x to the caller. So return void; would return the value void to the caller - not possible by definition.

This is different for null for example, as null is a valid value for reference types.

Stylistic question concerning returning void

I agree with you, the first style is confusing because there's the implication that some sort of value is getting returned. In fact I had to read it over a couple times because of that.

When returning from a function prototyped void, it should just be return;

What happens if void type function returns nothing?

C++11(ISO/IEC 14882:2011) §6.6.3 The return statement

A return statement without an expression can be used only in functions that do not return a value, that is, a function with the return type void, a constructor, or a destructor. A return statement with an expression of non-void type can be used only in functions returning a value

C11(ISO/IEC 9899:201x) §6.8.6.4 The return statement

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

However, C89/C90 only constraints on half of it:

C89/C90(ISO/IEC 9899:1990) §3.6.6.4 The return statement

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

In the C11 Forward section, it lists all the major changes in the third(i.e, C11) and second edition(i.e, C99). The last one of them is:

Major changes in the second edition included:

...

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

This means that the constraint change of function return type is changed since C99.

Why can't I explicitly return void from a method?

A return statement with an expression returns the value of that expression. The type of cancel() is a void expression - it doesn't have a value.

Logically you want to execute cancel(), and then return - so that's what you have to say. The two actions (calling cancel() and then returning) are logically distinct.

Now Java could have a sort of "unit" type instead of void - but that would affect rather more than just return values.



Related Topics



Leave a reply



Submit