### 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.

nlocal(x)

{

local y;

y = nlocal2(x);

y += x;

return(y);

}

nlocal2(a)

{

local b;

nonlocal x;

b = a + 1;

x = 10;

return(b);

}

The variable x is an argument or formal variable of nlocal. Variable x is also a nonlocal variable in nlocal2. When nlocal2 is called from nlocal, x refers to the formal variable of nlocal and is modified by nlocal2.

a = nlocal(0);

a == 11

Each calling function is searched bottom up to find the value of a nonlocal variable. If the nonlocal is not found, the value of a global variable of the same name is returned. Finally, an error results if no global variable is found.

x = 0;

a = nlocal2(0);

a == 1

x == 10

Nonlocal variables are useful for communicating values between SPL functions and custom dialog boxes without resorting to global variables.

// getreal.spl

static rval   = 0;

static rlimit = 0;

getreal(limit)

{

rlimit = limit;

return(rval);

}

// getreal.pan

@panel

@form

Input Real

<>

Real: <w=20>~rval = <{rval}>~input(1)

<L>

~rval = limit_real(rval)

@endform

nonlocal rlimit;

limit_real(val)

{

if (val < limit)

{

val = rlimit;

}

return(val);

}

a = getreal();

If the entered value is less than zero, a == 0, else a equals the entered value.

In this example, the variable rlimit is static to getreal.spl but is available to limit_real() because it is invoked by getreal and declared as nonlocal. The variable rlimit is private to getreal so it cannot conflict with global variables of the same name but it is accessible by limit_real in the dialog form of getreal.pan just as if it were a global variable.

See Forms: Including SPL Code in a Dialog Box for further details on mixing SPL routines and custom dialog box templates.