Local Variables in Scilab
In Scilab the local variables hold a unique position. They're defined exclusively within a function and remain confined to that space, invisible to the outside world.
What does this mean in practice? Simply put, you can't peek at or tweak a local variable from outside its parent function.
To shed light on this, let's walk through a tangible example.
Consider the following Scilab script:
- x = 1
- function myFunction()
- global x
- x=2
- disp(msprintf('x= %d', x))
- endfunction
- myFunction()
- disp(msprintf('x= %d', x))
At the outset, our script assigns the modest value of 1 to the variable `x`.
Venturing inside `myFunction()`, we declare a local variable named "x", bestowing upon it the value 2.
This value is then promptly displayed, resulting in the output:
x=2
Upon the function's conclusion, Scilab gracefully returns control to the main script.
The script, in turn, reveals the value of "x" to us, producing the output:
x=1
This exercise underscores a pivotal point: within its defining function, our local variable 'x' proudly bears the value 2.
Yet, once we step outside, its value reverts to a steadfast 1, unaffected by the internal machinations of the function.