Next: Status of Variables, Previous: Global Variables, Up: Variables
A variable that has been declared persistent within a function will retain its contents in memory between subsequent calls to the same function. The difference between persistent variables and global variables is that persistent variables are local in scope to a particular function and are not visible elsewhere.
A variable may be declared persistent using a persistent
declaration statement. The following statements are all persistent
declarations.
persistent a persistent a b persistent c = 2 persistent d = 3 e f = 5
The behavior of persistent variables is equivalent to the behavior of
static variables in C. The command static
in octave is also
recognized and is equivalent to persistent
. Unlike global
variables, every initialization statement will re-initialize the
variable. For example, after executing the following code
persistent pvar = 1 persistent pvar = 2
the value of the persistent variable pvar
is 2.