190 Creating and Running Algorithms Chapter 6
Defining andAccessing Global
Varia bles
Global variables are those declared outside of both the main() function and
any algorithms (see Figure 6-1). A global variable can be read or changed by
any algorithm. To declare global variables, use the command:
ALG:DEF 'GLOBALS','<source_code>'
where <source_code> is Algorithm Language source limited to constructs
for declaring variables. It must not contain executable statements.
Examples:
declare single variable without assignment
ALG:DEF 'GLOBALS','static float glob_scal_var;'
declare single variable with assignment
ALG:DEF 'GLOBALS','static float glob_scal_var = 22.53;'
declare one scalar variable and one array variable
ALG:DEF 'GLOBALS','static float glob_scal_var, glob_array_var[12];'
Global variables are accessed within an algorithm like any other variable.
glob_scal_var = P_factor * I108
NOTES 1. All variables must be declared static float.
2. Array variables cannot be assigned a value when declared.
3. All variables declared within an algorithm are local to that algorithm.
If a variable with the same identifier as an existing global variable is
locally declared, the algorithm will access the local variable only.
DeterminingFirst Execution(First_loop)
The VT1422A always declares the global variable first_loop. First_loop is
set to 1 each time INIT is executed. After main() calls all enabled algorithms,
it sets First_loop to 0. By testing First_loop, an algorithm can determine if
it is being called for the first time since an INITiate command was received.
Example:
static float scalar_var;
static float array_var [ 4 ];
/* assign constants to variables on first pass only */
if ( First_loop )
{scalar_var = 22.3;
array_var[0] = 0;
array_var[1] = 0;
array_var[2] = 1.2;
array_var[3] = 4;
}