What are LOCAL variables and why do I care?

LOCAL variables are somewhat like PRIVATE variables in that they
do not "scope up"; if you declare a PRIVATE or LOCAL variable in
function B which is called from function A, then function A does
not know the variable exists. Function A is even free to use the
same name for a different variable entirely without risk of
confusion. This is useful for writing functions that can avoid
"stepping on" the variables that are floating around a larger
application.

LOCAL variables, introduced with Clipper 5.0, enforce a stricter
kind of modularity. A LOCAL variable must be declared, and it
is visible only to the particular function in which it is
declared. (Remember that PRIVATEs are created by default.) Not
only does a LOCAL variable refrain from "up-scoping" but it also
will not "down-scope" either.

For example:

// LOCAL1.PRG
FUNCTION caller
LOCAL a
PRIVATE x, y
a := "a"
x := "ABC"
y := "123"
? a, x, y // Prints "a ABC 123"
callee()
? a, x, y // Prints "a ABC 456"
RETURN NIL

FUNCTION callee
LOCAL a
PRIVATE x // Notice y not declared PRIVATE!
x := "DEF"
y := "456"
? a, x, y // Prints "NIL DEF 456"
RETURN NIL


The values of (a) in the two functions are completely
independent of each other, because they are indeed two
separate variables. The values of (x) in the two functions
are also separate and independent, because CALLEE() has declared
its version of (x) PRIVATE. There is only one (y) variable,
however, since CALLEE() has neglected to declare it.

LOCAL variables are useful for writing code that is guaranteed
bulletproof regardless of the application environment. LOCAL
variables (and their cousins, the STATICs) are immune from having
their values changed by unrelated underlying functions. This cuts
down on maintenance and debugging while offering some speed
advantages too.

Because all uses of a local variable are known to the compiler
(being all in one function) there is no need for an entry in the
run-time symbol table, which does save some time and memory during
program execution. A side effect is that the names of LOCAL
variables in a macro expression are not found; therefore

LOCAL x, y
x := "y"
? &x

will fail unless there is a PRIVATE or PUBLIC variable named (y).

LOCALs work just like "auto" or stack variables in C and C++.