Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

static local variables in Scilab

105 views
Skip to first unread message

Sciman

unread,
Apr 29, 2005, 4:48:17 PM4/29/05
to
Hi Scilab experts,
I am trying to achieve in Scilab the same effect as one gets in C or
C++ using "static" local varibles. For example, I would like to convert
to Scilab code the following C (C++) function:

/* C function foo: */
int foo(int new_state)
{
static int state= 0;
if( new_state >= 0) {
state= new_state;
}

return state;
}

Variable "state" is a local variable and, therefore, is visible only
inside foo. On the other hand, "state" is static and it retains its
value in-between
foo invocations.

I have difficulties to obtain the same behavior in Scilab. According to
my (limited) knowledge, Scilab has only two types of variables:

* Local variables that are visible only inside its work-space. This
provides sufficient encapsulation. Unfortunately, Scilab local
variables disappear when function returns and, thus, do not persist
between function invocations. Too bad!

* Global variables persist between function invocations but are visible
everywhere and can be accidently overriden due to naming conflicts.
This could potentially result in very hard to find bugs.

Am I missing something? What is the canonical solution in Scilab for
the functions that want to remember its state between invocations??

Thanks,
--Leo--

Bruno

unread,
Apr 30, 2005, 2:09:46 AM4/30/05
to
Sciman wrote:
> Hi Scilab experts,
> I am trying to achieve in Scilab the same effect as one gets in C or
> C++ using "static" local varibles. For example, I would like to convert
> to Scilab code the following C (C++) function:
>
> /* C function foo: */
> int foo(int new_state)
> {
> static int state= 0;
> if( new_state >= 0) {
> state= new_state;
> }
>
> return state;
> }
>
> Variable "state" is a local variable and, therefore, is visible only
> inside foo. On the other hand, "state" is static and it retains its
> value in-between
> foo invocations.
>
> I have difficulties to obtain the same behavior in Scilab. According to
> my (limited) knowledge, Scilab has only two types of variables:
>

> * Global variables persist between function invocations but are visible


> everywhere and can be accidently overriden due to naming conflicts.
> This could potentially result in very hard to find bugs.
>

For a function to access a global variable, one must
do :

function y=foo(x)
// a function foo using a global var toto
global toto // now toto could be accessed by foo

So if you use a complicated name then the potential
conflicts are not likely to occur. Your C example
may be written like :


global foo_state
foo_state = 0; // to initialize
clear foo_state // delete foo_state (as a local var)
// but it exists always as a global var
function st = foo(new_state)
global foo_state
if new_state >= 0 then
foo_state = new_state
end
st = foo_state
endfunction // foo

You can put the function and the four lines
before in one file to exec (not getf because
of the 3 first lines) and so the initialization
would be done).

Note that in scilab all vars of the calling environment
(but not in the global stack) are readable but not writeable.
Also global variables are a lot strange because when you play
with a global var, a local var is created (and so the
clear foo_state). I remember some threads about global var
and so a search on this newsgroup could bring you others
informations.

hth
Bruno

0 new messages