34,333
edits
Changes
no edit summary
== Static Variables in Functions ==
In the normal course of program execution, any variables declared locally in a function are discarded when the function exits and execution returns to the code location from where the call was made. For example, each time the ''displayit'' function is called in the following code, variable ''jy'' is re-initialized to 0:
<pre>
void displayit (int i)
{
y += i;
</pre>
If we want the value of ''jy'' to be retained after the function has executed, we simply declare the variable as ''static'':
<pre>
</pre>
Now, when the code is compiled and run, we get the following output because the value of ''jy'' is not being reset to 0 each time the function is called:
<pre>