Re: Creating local variables in RPL Message #4 Posted by James M. Prange (Michigan) on 22 Apr 2007, 3:34 a.m., in response to message #3 by Ron Allen
In UserRPL:
A procedure (program or algebric object) can use any local variable already existing or defined for it, provided that a more recently created local variable with the same name doesn't exist.
In other words, an "inner" procedure can use a local variable defined for any "outer" procedure, but an outer procedure can't use any local variable defined only for its inner procedures.
A new, entirely separate, local variable can be defined with the same name as an already existing local variable. Only the most recently created (inner-most) existing local variable with a particular name is accessible.
Note that the index of FOR loop is actually a local variable, existing for as long as the loop is running, so suppose that I define a local variable n for a program, and then use n as a FOR loop index name; in that case, any use of n within the loop uses the loop index, not the local variable n previously defined for the program.
Also, local names can be the same as command names, library command names, or global names. So you could have a local variable named, for example, SIN, or i, or e, in which case only the local variable could be accessed, not the command with the same name.
If you use the HALT command, or various other commands that suspend program execution, any existing local variable is retained, and may be used while the "suspended environment" exists, as long as a newer local variable with the same name doesn't exist.
I'd forgotten about "compiled local variables". Starting with the 48G series, any name beginning with a <- character will be compiled as a local name, even if a local variable with that name doesn't currently exist and isn't being created. This simplifies using a local variable within a program (to be stored in a global variable) which will be called by name from within another program which defines that local variable. But note that compiling the name as local doesn't actually create a local variable; to use a local variable, it has to be currently defined.
If you try to evaluate a local name for which no local variable is currently defined, you get (what else?) an "Undefined Local Name" error.
Regards, James
|