next up previous contents index
Next: Predefined Constants Up: The Xic Scripting Language Previous: The exec Keyword   Contents   Index

Static and Global Variables

Variables defined in script functions are automatic by default. The term ``automatic'' means that every call of the function provides a fresh set of variables. A static variable, on the other hand, retains its contents between calls, and the same variable storage is used in all calls to the function. One can explicitly assign a variable in a function to be static using the static keyword. This construct should appear only in functions (not the main procedure), and must appear ahead of all other executable statements. The syntax is

static var1 [= val] var2 ...

The terms can be separated by white space and/or commas. The var1, etc., are variables used in the function that are to have static storage. They can optionally be initialized by including an assignment. If an assignment is used, the right hand side should consist of constants and variables that have already been assigned, meaning that they appear to the left in the present line or in a previous static line (there can be more than one). Array variables should have an initial dimensionality/size specification consisting of comma-separated integers enclosed in square brackets. Each such integer represents the maximum index for the dimension, with the lowest dimension listed to the left. This is the standard syntax for array declaration.


function myfunc(a, b, c)
static callcnt = 0
callcnt = callcnt + 1
Print("myfunc has been called", callcnt, "times")

There is also provision for global variables. Global variables are variables whose scope extends to all functions where the variables have been declared, including the main procedure. These are useful for data items that are accessed frequently throughout a script application.

The global keyword is used to declare global variables. The syntax is identical to that for the static keyword, and similarly the declaration must appear at the top of a function and the main procedure. There can be more than one global line.

global var1 [= value] var2 ...

In functions, the list following the keyword can not contain assignments or array subscripting. As with static declarations, global declarations must appear at the top of the function body. There can be multiple global lines, and these can be freely mixed with static lines. Global variables are not accessible unless declared.

A global variable must be declared in each function where it is to be accessed, and in the main procedure. Assignments and array initialization can be applied in the declarations in the main procedure only. It is an error to declare a global with assignment more than once, or to declare with an assignment in a function. Like other variables, if a global variable is not initialized in a declaration, the first assignment will define the variable type. Global array variables must be initialized with the maximum initial indices in each dimension, comma separated, enclosed in square brackets in the main function, but indices should not appear in the declarations in functions.


function myfunc()
    global gvar
    gvar = gvar + 1

global gvar = 1
# output is:
# 1
# 2

Global variables declared in functions create links to the global variable of the same name declared in the main procedure. If the function is defined in a separate file from the main procedure, such as a library file, and a global variable is declared and used in the function that is not also declared in the main procedure, an error results.

next up previous contents index
Next: Predefined Constants Up: The Xic Scripting Language Previous: The exec Keyword   Contents   Index
Stephen R. Whiteley 2022-05-28