Mercurial > octave-nkf
diff doc/interpreter/var.txi @ 6899:110c5782fe3b
[project @ 2007-09-14 15:17:53 by jwe]
author | jwe |
---|---|
date | Fri, 14 Sep 2007 15:17:53 +0000 |
parents | 5c9c49c51302 |
children | fd42779a8428 |
line wrap: on
line diff
--- a/doc/interpreter/var.txi Fri Sep 14 05:21:57 2007 +0000 +++ b/doc/interpreter/var.txi Fri Sep 14 15:17:53 2007 +0000 @@ -171,9 +171,29 @@ 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 @code{persistent} -declaration statement. The following statements are all persistent -declarations. +The following example uses a persistent variable to create a function +that prints the number of times it has been called. + +@example +@group +function count_calls () + persistent calls = 0; + printf ("'count_calls' has been called %d times\n", ++calls); +endfunction + +for i = 1:3 + count_calls (); +endfor + +@print{} 'count_calls' has been called 1 times +@print{} 'count_calls' has been called 2 times +@print{} 'count_calls' has been called 3 times +@end group +@end example + +As the example shows, a variable may be declared persistent using a +@code{persistent} declaration statement. The following statements are +all persistent declarations. @example @group @@ -186,8 +206,9 @@ The behavior of persistent variables is equivalent to the behavior of static variables in C. The command @code{static} in octave is also -recognized and is equivalent to @code{persistent}. Like global -variables, a persistent variable may only be initialized once. +recognized and is equivalent to @code{persistent}. + +Like global variables, a persistent variable may only be initialized once. For example, after executing the following code @example @@ -200,6 +221,72 @@ @noindent the value of the persistent variable @code{pvar} is 1, not 2. +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. + +@example +@group +function count_calls () + persistent calls; + if (isempty (calls)) + calls = 0; + endif + printf ("'count_calls' has been called %d times\n", ++calls); +endfunction +@end group +@end example + +@noindent +This implementation behaves in exactly the same way as the previous +implementation of @code{count_calls}. + +The value of a persistent variable is kept in memory until it is +explicitly cleared. Assuming that the implementation of @code{count_calls} +is saved on disc, we get the following behaviour. + +@example +@group +for i = 1:2 + count_calls (); +endfor +@print{} 'count_calls' has been called 1 times +@print{} 'count_calls' has been called 2 times + +clear +for i = 1:2 + count_calls(); +endfor +@print{} 'count_calls' has been called 3 times +@print{} 'count_calls' has been called 4 times + +clear all +for i = 1:2 + count_calls(); +endfor +@print{} 'count_calls' has been called 1 times +@print{} 'count_calls' has been called 2 times + +clear count_calls +for i = 1:2 + count_calls(); +endfor +@print{} 'count_calls' has been called 1 times +@print{} 'count_calls' has been called 2 times +@end group +@end example + +@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} 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} function as described in +@xref{Function Locking}. + @node Status of Variables @section Status of Variables