This article needs to beupdated. Please help update this article to reflect recent events or newly available information.(November 2025) |
Incomputer science, alocal variable is avariable that is givenlocalscope. A local variable reference in thefunction orblock in which it is declared overrides the same variable name in the larger scope. Inprogramming languages with only two levels of visibility, local variables are contrasted withglobal variables. On the other hand, manyALGOL-derived languages allow any number of nested levels of visibility, with private variables, functions, constants and types hidden within them, either by nested blocks ornested functions. Local variables are fundamental toprocedural programming, and more generallymodular programming: variables of local scope are used to avoid issues withside-effects that can occur withglobal variables.
Local variables may have a lexical or dynamicscope, though lexical (static) scoping is far more common. In lexical scoping (or lexical scope; also called static scoping or static scope), if a variable name's scope is a certain block, then its scope is the program text of the block definition: within that block's text, the variable name exists, and is bound to the variable's value, but outside that block's text, the variable name does not exist. By contrast, in dynamic scoping (or dynamic scope), if a variable name's scope is a certain block, then its scope is that block and all functions transitively called by that block (except when overridden again by another declaration); after the block ends, the variable name does not exist. Some languages, likePerl andCommon Lisp, allow the programmer to choose static or dynamic scoping when defining or redefining a variable. Examples of languages that use dynamic scoping includeLogo,Emacs lisp, and the shell languagesbash,dash, and the MirBSD Korn shell (mksh)'s "local" declaration. Most other languages provide lexically scoped local variables.
In most languages, local variables areautomatic variables stored on thecall stack directly. This means that when arecursive function calls itself, local variables in each instance of the function are given distinctaddresses. Hence variables of this scope can be declared, written to, and read, without any risk ofside-effects to functions outside of the block in which they are declared.
Programming languages that employcall by value semantics provide a called subroutine with its own local copy of thearguments 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 andcall 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.
A special type of local variable, called astatic local, is available in many mainstream languages (includingC/C++,Visual Basic,VB.NET andPHP) which allows a value to be retained from one call of the function to another – it is astatic variable with local scope. In this case, recursive calls to the function also have access to the (single,statically allocated) variable. In all of the above languages, static variables are declared as such with a specialstorage class keyword (e.g.,static).
Static locals in global functions have the same lifetime asstatic global variables, because their value remains in memory for the life of the program,[1] but havefunction scope (not global scope), as with automatic local variables.
This is distinct from other usages of thestatic keyword, which has several different meanings in various languages.
Perl supports both dynamic and lexically-scoped local variables. The keywordlocal is used to define local dynamically-scoped variables, whilemy is used for local lexically-scoped variables. Since dynamic scoping is less common today, the Perl documentation warns that "local isn't what most people think of as “local”.".[2] Instead, thelocal keyword 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.[3] To create lexically-scoped local variables, use themy operator instead.[4]
To understand how it works consider the following code:
$a=1;subf(){local$a;$a=2;g();}subg(){print"$a\n";}g();f();g();
this will output:
121
This happens since the global variable $a is modified to a newtemporary (local) meaning insidef(), but the global value is restored upon leaving the scope off().
Usingmy in this case instead oflocal would have printed 1 three times since in that case the$a variable would be limited to the static scope of the functionf() and not seen byg().
Randal L. Schwartz and Tom Phoenix argue that the operatorlocal should have had a different name likesave.[5]
Ruby as a language was inspired also by Perl, but in this case, the notation was made simpler: a global variable name must be preceded by a $ sign, like$variable_name, while a local variable has simply no $ sign in front of its name, likevariable_name (while in perl all scalar values have a $ in front). Note that Ruby only provides built-in support for statically-scoped local variables like Perl'smy, not dynamically-scoped local variables like Perl'slocal. There is at least one library for Ruby that provides dynamically-scoped variables.[6]
local()my()