#include <iostream> using namespace std; void showLocal(); //Function prototype int main() { showLocal(); showLocal(); return 0; } void showLocal() { int localNum = 5; // Local variable cout << "localNum is " << localNum << endl; localNum = 99; }程式輸出結果:
localNum is 5
localNum is 5
#include <iostream> using namespace std; void showStatic(); // Function prototype int main() { // Call the showStatic function five times for (int count = 0; count < 5; count++) showStatic(); return 0; } void showStatic() { static int numCalls = 0; // Static local variable cout << "This function has been called " << ++numCalls << " times." << endl; }程式輸出結果:
This function has been called 1 times.
This function has been called 2 times.
This function has been called 3 times.
This function has been called 4 times.
This function has been called 5 times.