Understanding Variable Scope in a Script

In order to effectively use Script elements in GoldSim, it is important to understand the concept of variable scope.

When a variable is defined in a script (either by a Variable Definition statement, or by a Do or For loop, which implicitly define loop variables), the variable persists (and hence can be referenced) from the time it is defined to the statement that closes the scope in which the variable is defined.  

The scope of script variables that are 1) not inside a loop; and 2) not inside an If-Else statement consists of all lines of the script below the definition. That is, they persist until the last statement in the script.

However, script variables defined in loops or If-Else statements do not persist throughout the remainder of the script.  Their scope is limited to the the loop or If-Else clause where they are defined.

To illustrate this, let’s first consider a simple Do loop:

Within this script, there are actually two variables that have been defined: 1) n is defined implicitly by the Do loop; and 2) k is defined explicitly by a Variable Definition statement. For both of these variables, the scope is the Do loop itself.  The scope does not extend beyond the Do loop.  Hence, neither n nor k can be referenced outside the loop.  In the example, k is referenced outside of the loop, and this generates an error (line 5 is displayed in red, and the model will not run).

In order to reference k outside the loop, it would have to be redefined after the loop:

In the example above, k ceased to exist after line 4, and then was redefined at line 5.  Hence, the two scopes do not overlap. Note that defining k before (and within the loop) would have generated an error.  That is, the following script is invalid:

In this case, line 3 is displayed in red and marked as invalid.  The reason for this error is that since k was defined in line 1, its scope is the entire script. Hence, when we try to define it again in line 3, an error is generated because the two scopes overlap.  Defining the same variable twice within the same scope generates a redefinition error. In the previous example, there was no error because the scopes did not overlap.

This same logic also applies for the other looping constructs in GoldSim (For loops, While loops, and Repeat-Until loops).

If-Else statement blocks also create local scopes.  In this case, the scope of any variables inside an If-Else statement block is not the entire block; rather, each clause creates local scopes for any variables that are defined within them.  For example, consider the following script:

In this example, the local script variable Var1 is created in the first clause. The scope of this variable is only that clause (lines 2 and 3).  Hence, it cannot be referenced outside of that clause (without redefining it). Referencing it in the second clause generates an error.

If you wanted to reference it throughout the entire If-Else block, you would need to define it prior to the If-Else block:

Related Topics…

Learn more about: