[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
static local variables
From: |
John W. Eaton |
Subject: |
static local variables |
Date: |
Fri, 21 Mar 1997 10:31:04 -0600 |
On 21-Mar-1997, Peter Bruhn <address@hidden> wrote:
| is there some way to have local variables keep their values, after the
| function has been ended and restarted?
Currently, the only way to do this is by using global variables.
| I would like to have this, because I have a lot of sub-functions programmed
| by different people. And I do not want to insert global variables into the
| main-function whenever a sub-function is changed.
You don't need to declare the variables global in the main function.
For example, this function keeps track of the number of times it is
called:
function f ()
global f_static_ncalls;
if (! exist ("f_static_ncalls"))
f_static_ncalls = 0;
endif
++f_static_ncalls
## ...
endfunction
Using a name like FCN_static_FOO helps to avoid name clashses with
real global variables.
Also, it is necessary to use call exist() to decide when to initialize
the variable. Writing
global f_static_ncalls = 0;
*should* work to initialize the variable just once, but it doesn't.
This is a bug, and should eventually be fixed.
Finally, I realize that this is not really the Right Thing, so the
PROJECTS file contains the following entry:
* Make it possible to have `static' variables inside functions that
retain their values across calls.
Perhaps this will be added in the next release. Maybe sooner, if
someone contributes the code. :-)
Thanks,
jwe