Static function variable : Function Variables « Function « C++






Static function variable

Static function variable

#include <iostream>
using namespace std;
void printMessage(void);  

int main ()
{
   char choice;
   do {
      cout << "Enter Q to quit, any other character to continue: ";
      cin >> choice;
      if (choice == 'Q')
         cout << "Input stopped";
      else
         printMessage(); 
      } while (choice != 'Q');
   return 0;
}
void printMessage (void)
{
   static int times = 0;
   times++;
   cout << "This function called " << times << " times\n";
}

           
       








Related examples in the same category

1.Returning a Pointer to a Static Local VariableReturning a Pointer to a Static Local Variable
2.Function local variableFunction local variable