In most languages, local variables are automatic variables stored on the call stack directly. This means that when a recursive function calls itself, local variables in each instance of the function are given separate memory address space. Hence variables of this scope can be declared, written to, and read, without any risk of side-effects to processes outside of the block in which they are declared.
Programming languages that employ call by value semantics provide a called subroutine with its own local copy of the arguments passed to it. In most languages, these local parameters are treated the same as other local variables within the subroutine. In contrast, call by reference and call by name semantics allow the parameters to act as aliases of the values passed as arguments, allowing the subroutine to modify variables outside its own scope.
Some advocate that all variables should be of local scope to avoid issues with side-effects.
Static locals in global functions can be thought of as global variables, because their value remains in memory for the life of the program. The only difference is that they are only accessible through one function. Static locals can also be declared in class-level functions in object-oriented languages.
Stricter and more formal object-oriented languages such as Java and C#, do not allow local variables to be declared static to a function. Instead, "static" variables in these languages are scoped to the class.
Note: This is distinct from other usages of the
static keyword, which has several different meanings in various other languages.
Perl has a keyword,
local, for “localizing” variables, but in this case,
local isn't what most people think of as “local”. It gives a temporary, dynamically-scoped value to a global (package) variable, which lasts until the end of the enclosing block. However, the variable is visible to any function called from within the block.
To create lexical variables, which are more like the automatic variables discussed above, use the
my operator instead.
US Patent Issued to Electronics and Telecommunications Research Institute on July 2 for "Accessing Value for Local Variable from Function Call Stack upon Offset Matching with Instruction Extracted Stack Pointer Offset or from Cache" (South Korean Inventors)
Jul 02, 2013; ALEXANDRIA, Va., July 2 -- United States Patent no. 8,478,970, issued on July 2, was assigned to Electronics and...
US Patent Issued to International Business Machines on Sept. 25 for "Method and System for Dynamic Loop Transfer by Populating Split Variables" (Canadian Inventors)
Sep 27, 2012; ALEXANDRIA, Va., Sept. 27 -- United States Patent no. 8,276,131, issued on Sept. 25, was assigned to International Business...