Defining Variables

 

In general, SPL variables do not require declaration by type. Assigning a value with the = operator creates the variable if it did not previously exist, and establishes its data type. Variable names are not case sensitive and may be up to 128 characters in length. Variables may assume any of the following data types:

 

 

Examples:

 

a = 10;   

// integer

b = 3.14;

// real

c = 1 + 5i;

// complex

d = "This is a string";

// string

h = 0x100;     

// integer in hex form

s = {1, 2, 3, 4};

// 4 element series

t = {{1, 2, 3}, {4, 5, 6}};

// 2 x 3 array

 

 

Local Variables

 

Variables used within a user defined function are local to that function. When the evaluation of the function is completed, the local variables defined by the function are automatically deleted.

 

fun1(x)

{

    local a;

 

    a = x + 1;

 

    return(a);

}

 

Now consider the following interactive session:

 

a = 1 

fun1(10)  returns: 11

a         returns: 1

 

The variable a referenced in function fun1 is local to the function. The variable a defined by the expression a = 1 is global to the session. The local variable a defined in fun1 does not conflict with the global variable a defined in the session.

 

An undeclared variable assigned in an SPL function is treated as a local variable.  Thus, the local keyword is not strictly necessary, but is extremely useful for eliminating a conflict between a local variable name and a function or macro. The keyword also provides optimization benefits as the type of variable is made explicit.

 

The value of an undeclared variable referenced in an SPL is first searched as a local variable. If not found, the variable is treated as a global variable.

 

Explicit Global Variables

 

To explicitly manipulate a global variable within a function, use the setvar and getvar routines or declare the variable as extern.

 

fun2(x)

{

    local y;

 

    y = getvar(a);

    

    setvar(a, x + y);

 

    return(a);

}

 

a = 1;

 

fun2(10)  returns: 11

a         returns: 11

 

The fun2 function explicitly gets and sets the value of the global variable a. A global variable can also be specified using the extern or global keyword.

 

fun3(x)

{

    local y;

    extern a;

 

    y = a;

    a = x + y;

 

    return(a);

}

 

Function fun3 performs identically to fun2 but uses a cleaner, more compact syntax.

 

Static Variables

 

A static variable behaves exactly like a local variable except a static variable is persistent across function calls.

 

sumval(x)

{

    static sum = 0;

 

    sum += x;

 

    return(sum);

}

 

The function sumval returns the sum of the current input value and all previous values. The expression sum += x adds the value of x to the current value of sum. The static variable sum behaves like a local variable except it is persistent from one call to sumval to another.

 

Static variables can be file based such that all functions within the same SPL file have access to the variable.

 

// sumval.spl

 

static sum = 0;

 

sumval(x)

{

    sum += x;

 

    return(sum);

}

 

currsum()

{

    return(sum);

}

 

In this case, sumval behaves identically to the previous example and currsum returns the current sum.

 

Static variables are initialized when the SPL function is first loaded, whether automatically loaded or explicitly loaded. If no initialization value is given, the static variable is initialized to 0.

 

A common use of static variables is to initialize the state of a function.

 

mycalc(x)

{

    static first = 1;

    local y;

 

    if (first)

    {

        do_initialization();

        first = 0;

    }

 

    y = do_calculation(x);

 

    return(y);

}

 

The initialization function do_initialization is called the very first time mycalc is called and is skipped otherwise.

 

Nonlocal Variables

 

A nonlocal variable is a variable not defined in the current SPL routine. All global variables are nonlocal variables. However, a nonlocal variable in one SPL routine can be a local or static variable of another SPL routine. See Nonlocal Variables for more details.