<html lang="en"> <head> <title>Persistent Variables - Untitled</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Untitled"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Variables.html#Variables" title="Variables"> <link rel="prev" href="Global-Variables.html#Global-Variables" title="Global Variables"> <link rel="next" href="Status-of-Variables.html#Status-of-Variables" title="Status of Variables"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Persistent-Variables"></a> <p> Next: <a rel="next" accesskey="n" href="Status-of-Variables.html#Status-of-Variables">Status of Variables</a>, Previous: <a rel="previous" accesskey="p" href="Global-Variables.html#Global-Variables">Global Variables</a>, Up: <a rel="up" accesskey="u" href="Variables.html#Variables">Variables</a> <hr> </div> <h3 class="section">7.2 Persistent Variables</h3> <p><a name="index-persistent-variables-427"></a><a name="index-g_t_0040code_007bpersistent_007d-statement-428"></a><a name="index-variables_002c-persistent-429"></a><a name="doc_002dpersistent"></a>A variable that has been declared <dfn>persistent</dfn> 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. <p>The following example uses a persistent variable to create a function that prints the number of times it has been called. <pre class="example"> function count_calls () persistent calls = 0; printf ("'count_calls' has been called %d times\n", ++calls); endfunction for i = 1:3 count_calls (); endfor -| 'count_calls' has been called 1 times -| 'count_calls' has been called 2 times -| 'count_calls' has been called 3 times </pre> <p>As the example shows, a variable may be declared persistent using a <code>persistent</code> declaration statement. The following statements are all persistent declarations. <pre class="example"> persistent a persistent a b persistent c = 2 persistent d = 3 e f = 5 </pre> <p>The behavior of persistent variables is equivalent to the behavior of static variables in C. The command <code>static</code> in Octave is also recognized and is equivalent to <code>persistent</code>. <p>Like global variables, a persistent variable may only be initialized once. For example, after executing the following code <pre class="example"> persistent pvar = 1 persistent pvar = 2 </pre> <p class="noindent">the value of the persistent variable <code>pvar</code> is 1, not 2. <p>If a persistent variable is declared but not initialized to a specific value, it will contain an empty matrix. So, it is also possible to initialize a persistent variable by checking whether it is empty, as the following example illustrates. <pre class="example"> function count_calls () persistent calls; if (isempty (calls)) calls = 0; endif printf ("'count_calls' has been called %d times\n", ++calls); endfunction </pre> <p class="noindent">This implementation behaves in exactly the same way as the previous implementation of <code>count_calls</code>. <p>The value of a persistent variable is kept in memory until it is explicitly cleared. Assuming that the implementation of <code>count_calls</code> is saved on disk, we get the following behavior. <pre class="example"> for i = 1:2 count_calls (); endfor -| 'count_calls' has been called 1 times -| 'count_calls' has been called 2 times clear for i = 1:2 count_calls(); endfor -| 'count_calls' has been called 3 times -| 'count_calls' has been called 4 times clear all for i = 1:2 count_calls(); endfor -| 'count_calls' has been called 1 times -| 'count_calls' has been called 2 times clear count_calls for i = 1:2 count_calls(); endfor -| 'count_calls' has been called 1 times -| 'count_calls' has been called 2 times </pre> <p class="noindent">That is, the persistent variable is only removed from memory when the function containing the variable is removed. Note that if the function definition is typed directly into the Octave prompt, the persistent variable will be cleared by a simple <code>clear</code> command as the entire function definition will be removed from memory. If you do not want a persistent variable to be removed from memory even if the function is cleared, you should use the <code>mlock</code> function as described in See <a href="Function-Locking.html#Function-Locking">Function Locking</a>. </body></html>