Using a static variable in a function - C++ Function

C++ examples for Function:static variable

Introduction

You can define a function's local variable to be static and set it an initial value.

The initialization is done only the first time the function is called, and the data retains its value between function calls.

Demo Code

#include <iostream>
using namespace std;
void func() {//from  w w w .j  a v  a 2s  .c  o m
   static int i = 0;
   cout << "i = " << ++i << endl;
}
int main() {
   for(int x = 0; x < 10; x++)
      func();
}

Result


Related Tutorials