Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a void-returning function g return f(); when f returns void?

Consider the following snippet:

void f(void);  void g(…) {   …   return f();   … } 

Is this return f(); valid according to C11?

I am not advocating using this pattern: if it works at all, it is obviously equivalent to f(); return; (where the return; itself would be redundant if this is at the end of function g()). I am asking this question in the context of the static analysis of C programs, where the C code has already been written by someone else and the question is deciding whether or not it is valid according to the standard.

I would interpret C11 6.8.6.4:1 as meaning that it is non-standard and should be statically rejected. Is it possible to interpret it differently (I have found this pattern in actual and otherwise high-quality source code)?

Constraints

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.

like image 509
Pascal Cuoq Avatar asked Mar 19 '14 14:03

Pascal Cuoq


People also ask

Can you return in a void function?

Return from void functions in C++ The void functions are called void because they do not return anything. “A void function cannot return anything” this statement is not always true. From a void function, we cannot return any values, but we can return something other than values.

Do void functions need a return statement?

A function that does not return a value is called a non-value returning function (or a void function). A void function will automatically return to the caller at the end of the function. No return statement is required.

What type does void return?

In computer programming, when void is used as a function return type, it indicates that the function does not return a value. When void appears in a pointer declaration, it specifies that the pointer is universal.

What is the return statement in a function returning void?

In a function returning void, the return statement with expression can be used, if the expression type is void. return without expression not permitted in function that returns a value (and vice versa) ( return with expression not permitted in function that returns a void)

What is a void function in C++?

Void functions are known as Non-Value Returning functions. They are “void” due to the fact that they are not supposed to return values. True, but not completely. We cannot return values but there is something we can surely return from void functions. Void functions do not have a return type, but they can do return values.

What is a function that does not return a value?

A function that does not return a value is called a non-value returning function (or a void function ). A void function will automatically return to the caller at the end of the function. No return statement is required.

Can a function return a value in C?

The functions are not necessary for returning a value. Moreover, to tell the compiler that a function does not return a value, a return type of void is applied. Let’s see at the doPrint () function: This function comes with a return type of void, representing that it does not return a value to the caller.


2 Answers

Anything after return is an expression.

6.8.6:1 Jump statements

 Syntax       ...    return expressionopt;  

And standard says that:

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

f() is also an expression here. The compiler should raise a warning

[Warning] ISO C forbids 'return' with expression, in function returning void [-pedantic] 
like image 157
haccks Avatar answered Sep 18 '22 20:09

haccks


This clearly is a constraint violation, in particular in view of

6.3.2.2 void: The (nonexistent) value of a void expression (an expression that has type void) shall not be used in any way,

That means that the incomplete type void is a dead end that cannot be reused for any purpose whatsoever.

like image 37
Jens Gustedt Avatar answered Sep 20 '22 20:09

Jens Gustedt