Friday, 16 December 2016

return function call inside a void function

C++ have some special feature that you may never used or heard of. Here is one example, you could return a void function call inside a void function.

jtony@genoa:~/learn$ xlC void.cpp
jtony@genoa:~/learn$ ./a.out
I am in FOO!
I am in BAR !
jtony@genoa:~/learn$ cat void.cpp
#include<iostream>
using namespace std;

void bar() {
  cout << "I am in BAR !" << endl;
}

void foo() {
  cout << "I am in FOO!" << endl;
  return bar();
}

int main(void) {
 foo();
 return 0;
}

According to link: http://stackoverflow.com/questions/2249108/can-i-return-in-void-function

Interestingly, you can also return void from a void function. For example:
void foo()
{
  return void();
}

No comments:

Post a Comment