Yay295
Yay295

Reputation: 1848

Calling a function from the return statement of a void function

If I return a void function from a void function, will that function be called before returning?

example:

#include <iostream>
void one ( ) { std::cout << "Hello world.\n"; }
void two ( ) { return one ( ); }
int main ( ) { two ( ); }

Will "Hello world." be printed to the screen?

Upvotes: 3

Views: 287

Answers (2)

user3920237
user3920237

Reputation:

Yes. From N3337 (draft C++11 standard):

[stmt.return]/3 A return statement with an expression of type voidcan be used only in functions with a return type of cv void; the expression is evaluated just before the function returns to its caller.

Upvotes: 4

Yay295
Yay295

Reputation: 1848

Yes, "Hello world." will be printed to the screen, as function one is called before function two returns.

enter image description here

Upvotes: 5

Related Questions