Content deleted Content added
No edit summary Tags: Reverted possible vandalism |
|||
(3 intermediate revisions by 3 users not shown) | |||
Line 1:
{{Short description|Computer programming, a variable
In [[computer science]], a '''local variable''' is a [[Variable (programming)|variable]] that is given ''local [[scope (programming)|scope]]''. A local variable reference in the [[subroutine|function]] or [[block (programming)|block]] in which it is declared overrides the same variable name in the larger scope. In [[programming language]]s with only two levels of visibility, local variables are contrasted with [[global variables]]. On the other hand, many [[ALGOL]]-derived languages allow any number of nested levels of visibility, with private variables, functions, constants and types hidden within them, either by nested blocks or [[nested function]]s. Local variables are fundamental to [[procedural programming]], and more generally [[modular programming]]: variables of local scope are used to avoid issues with [[side-effect (computer science)|side-effects]] that can occur with [[global variable]]s.
==Scope==
Local variables
In most languages, local variables are [[automatic variable]]s stored on the [[call stack]] directly. This means that when a [[recursion (computer science)|recursive function]] calls itself, local variables in each instance of the function are given distinct [[Memory address|addresses]]. Hence variables of this scope can be declared, written to, and read, without any risk of [[side-effect (computer science)|side-effects]] to functions outside of the block in which they are declared.
Line 10:
==Static local variables==
A special type of local variable, called a ''static local,'' is available in many mainstream languages (including [[C (programming language)|C]]/[[C++]], [[Visual Basic]], [[Visual Basic (.NET)|VB.NET]] and [[PHP]]) which allows a value to be retained from one call of the function to another – it is a [[static variable]] with local scope. In this case, recursive calls to the function also have access to the (single, [[static memory allocation|statically allocated]]) variable. In all of the above languages, static variables are declared as such with a special ''storage class'' keyword (e.g., <code>static</code>).
Static locals in global functions have the same lifetime as [[static global variable]]s, because their value remains in memory for the life of the program,<ref>{{cite web|url= http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf |title=Current C standard }} {{small|(3.61 MB)}} ({{As of|2009|lc=on}}). In particular, see section 6.2.4 “Storage durations of objects”, page 32.</ref> but have [[function scope]] (not global scope), as with automatic local variables.
Line 18:
==Local variables in Perl==
[[Perl]] supports both dynamic and lexically-scoped local variables. The keyword <code>local</code> is used to define local dynamically-scoped variables, while <code>my</code> is used for local lexically-scoped variables. Since dynamic scoping is less common today, the Perl documentation warns that "<code>local</code> isn't what most people think of as “local”.".<ref>[http://perldoc.perl.org/functions/local.html perldoc.perl.org: local]</ref> Instead, the <code>local</code> keyword gives a temporary, [[scope (
To understand how it works consider the following code:
|