Content deleted Content added
Tags: references removed Visual edit |
|||
(17 intermediate revisions by 14 users not shown) | |||
Line 1:
{{Short description|Computer programming, a variable only usable in a portion of a program (the scope)}}
In [[computer science]], a '''local variable''' is a [[Variable (programming)|variable]] that is given ''local [[scope (programming)|scope]]''.
==Scope==
Local variables may have a lexical or dynamic [[scope (programming)|scope]], 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, like [[Perl]] and [[Common Lisp]], allow the programmer to choose static or dynamic scoping when defining or redefining a variable. Examples of languages that use dynamic scoping include [[Logo (programming language)|Logo]], [[Emacs lisp]], and the shell languages [[Bash (Unix shell)|bash]], [[dash (shell)|dash]], and the MirBSD Korn shell ([[mksh]])'s "local" declaration. Most other languages provide lexically scoped 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 8 ⟶ 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]],
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 16 ⟶ 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:
<syntaxhighlight lang="perl">
$a = 1;
sub f() {
Line 29 ⟶ 34:
f();
g();
</syntaxhighlight>
this will output:
Line 38 ⟶ 43:
This happens since the global variable $a is modified to a new ''temporary'' (local) meaning inside {{code|f()}}, but the global value is restored upon leaving the scope of {{code|f()}}.
Using <code>my</code> in this case instead of <code>local</code> would have printed 1 three times since in that case the <code>$a</code> variable would be limited to the static scope of the function {{code|f()}} and not seen by {{code|g()}}.<br /> Randal L. Schwartz and Tom Phoenix argue that the operator <code>local</code> should have had a different name like <code>save</code>.<ref>{{cite book|author=Randal L. Schwartz and Tom Phoenix|title=Learning Perl 3rd edition|at=paragraph 4.7|publisher=O'REILLY
==Local variables in Ruby==
|