C++ examples for Function:static variable
Uses a static variable with the static declaration.
#include <iostream> using namespace std; int triple_it(int ctr); int main()/* w ww . j av a 2 s .co m*/ { int ctr; // Used in the for loop to call a function 25 times. for (ctr=1; ctr<=25; ctr++) { triple_it(ctr); } // Pass ctr to a function called triple_it(). return 0; } int triple_it(int ctr) { static int total=0; int ans; ans = ctr * 3; total += ans; cout << "The number " << ctr << " multiplied by 3 is " << ans << "\n"; if (total > 300) { cout << "The total of triple numbers is over 300 \n"; } return 0; }