Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is "return voidFunction()" valid code? [duplicate]

Tags:

c++

c

return

void

I found out that the following code gets accepted by Visual C++ 2008 and GCC 4.3 compilers:

void foo()
{

}

void bar()
{
  return foo();
}

I am a bit surprised that it compiles. Is this a language feature or is it a bug in the compilers? What do the C/C++ standards say about this?

like image 769
Karel Petranek Avatar asked Dec 31 '25 03:12

Karel Petranek


1 Answers

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.

like image 162
Cubbi Avatar answered Jan 01 '26 18:01

Cubbi