In this lesson we're going to talk about static 
variables.
Now, as you know, when a function is executed,
the parameters and local variables come into 
existence and then when control leaves the 
function
all those variables are ejected from the runtime 
stack, they disappear, they're no longer in 
existence.
They go out of scope, and are deallocated. Those 
are non-static variables.
You might be thinking, sometimes it would be 
really nice to be able to retain the value from a 
variable
in a function. You can do that with static variables.
Okay, so local variables that are normal variables, 
non-static variables, when they go out of scope 
they are deallocated but, static variables
remain and they retain their value.
So, here is the syntax for declaring a static 
variable. You use the keyword "static"
in front of the variable type and then, of course, 
the variable name. And we can initialize the value 
to whatever we want,
or we cannot. If we so choose, the initialization 
will be automatic.
So, the rules for static variables.
Number one: the declaration and initialization of a 
static variable
is executed once and only once. And that 
happens on the first execution of the function.
The initialization will happen only once and that 
will be the first time that it is executed. And unless 
you otherwise specify,
the value will be initialized to zero. In any 
subsequent invocation of the function,
will retain the value of that variable when it was
last set in the last execution of that function. So, 
let's take a look at some examples.
The first example here is kind of contrived. We 
have a static short that is initialized to one. So, it's 
created and initialized to one on the first
execution of the function. And we have an output 
statement
that says, "this function has been called"
count "time" or "times". So, the first execution will 
say, "this function has been called one time".
Notice then that the last statement is "count++", 
so our count is incremented.
So, the second execution of the function, it will 
say, "this function has been called two times" and 
then the third it will say, "this function has been
called three times", then "four times", and "five 
times", etc...
In our little main program here, we simply enter a 
loop, and that is the output of
that "for" loop. You might be thinking this is kinda 
a silly function, yes it is.
But, it maybe be that,
the number of times that the function has been 
called could play some important role in the 
calculation in that function.
And you can then use it for that. Besides just 
knowing how many times the function has been 
called.
In our next example, it is a little more useful.
"is_it_larger". Here we have the function where 
we declare a float called "largest".
It's static, so it's going to persist after the 
execution is finished executing.
and we initialize to "val". That means that the very 
first
argument that is sent to this function, the very 
first "val" sent to it, it's going to be the initial value 
of "largest". Now,
in every subsequent call the functions, we say if 
"largest" less than "val", then "largest" is assigned 
"val". That means that,
we're going to hold on to the largest value
that is sent to this function. Each time that's 
gonna be updated if the new argument sent is the 
largest.
Then output statements would simply say, "the 
largest value, sent to the function so far is," and 
then "largest". Okay.
Here is another, more interesting, example.
Now, in C++ you have the capability of generating 
random numbers.
We'll go over that capability in a later lesson, but
for right now I'm going to build a random number 
generator. We need to "#include ", so we 
have access to the time clock.
I'm going to call this function "my_rand". The built 
in function in the compiler is "rand" so you 
wouldn't want to call it that.
It's going to return a "long", we send nothing to it, 
we start by declaring a static
"long" called "seed", and we're going to initialize it 
to "time(NULL)". This value,
