Variable Scope: Global vs. Local

A variable's scope is the range of the script where it is visible. Variables have either global or local scope. A global variable exists only once in a script, and is visible in every function. Modifications to it in one function are permanent and visible to all functions. Unless declared otherwise, all variables in a script are global. Global variables are useful for values that are relatively constant, or that many functions in the script must access, such as a session id.

A local variable, however, has a limited scope: it exists only within the block that it is declared in. Once that block ends, the variable is destroyed and its values lost. A local variable of the same name declared elsewhere is a different variable. A local variable can even exist multiple times simultaneously, if its block is entered again before it's exited - i.e. a recursive function call. Each call of the function will have a distinct local variable.

Local variables must be explicitly declared, either as parameters to a script function (here), or with the LOCAL statement (here). They are used to clearly pass parameters to functions, or as temporary "scratch space" for a function without the side effects of global variable modification.

A local variable with the same name as another in-scope variable will have precedence over the outer variable. For example, a local variable named $x has precedence over, and will "hide", a global variable named $x:

<A NAME=main>
  <$x = "fragile">        <!-- global $x -->
  <LOCAL x>
    <$x = "test">         <!-- only local $x seen here -->
    ...
  </LOCAL>
  The value of x is $x    <!-- back to global $x -->
</A>

Within the LOCAL block, references to $x refer to the local variable: the global $x is hidden. Thus, the global $x value "fragile" is not lost when the LOCAL block ends.


Copyright © Thunderstone Software     Last updated: Apr 15 2024
Copyright © 2024 Thunderstone Software LLC. All rights reserved.