Declare and initialize a static variable as counter in a function (increment it in every call) and call it multiple times printing the change in value
#include <iostream>
using namespace std;
void someFunction()
{
static int counter = 0;
cout << "Calling function" << endl;
counter++;
cout << "counter = " << counter << endl << endl;
}
int main()
{
for(int i = 0; i < 5; i++)
someFunction();
return 0;
}
Comments
Leave a comment