You could use a static local variable to guard against the print happening multiple times:
void function()
{
static bool runOnce= true;
if(runOnce)
{
cout << //print stuff
runOnce = false;
}
... do other stuff
}
OR a C++11 fancy pants answer using a lambda, which is shorter but arguably less readable:
using namespace std;
void function()
{
static const auto runOnce = [] { cout << "Hello" << endl; return true;}();
}
int main()
{
function();
function();
function();
return 0;
}
Result:
Hello