C++ examples for Function:static variable
Demonstrate static variables
#include <iostream> using namespace std; float getavg(float); //declaration int main()/*from w w w.j ava 2 s. c o m*/ { float data=1, avg; while( data != 0 ) { cout << "Enter a number: "; cin >> data; avg = getavg(data); cout << "New average is " << avg << endl; } return 0; } // finds average of old plus new data float getavg(float newdata) { static float total = 0; //static variables are initialized static int count = 0; // only once per program count++; //increment count total += newdata; //add new data to total return total / count; //return the new average }