Content deleted Content added
m →Static local variables: link |
|||
(43 intermediate revisions by 27 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.
Programming languages that employ ''[[call by value]]'' semantics provide a called subroutine with its own local copy of the [[function argument|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.
==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>{{
This is distinct from other usages of the [[Static (keyword)|<code>static</code> keyword]], which has several different meanings in various languages.
==
[[Perl]] has a keyword, <code>local</code>, for “localizing” variables, but in this case, <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> It gives a temporary, [[scope (programming)|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.<ref>[http://perldoc.perl.org/perlsub.html#Temporary-Values-via-local() perldoc.perl.org: perlsub: Temporary Values via <code>local()</code>]</ref>▼
▲[[Perl]]
To understand how it works consider the following code:
<
$a = 1;
sub f() {
local $a;
$a = 2;
g();
}
sub g() {
print "$a\n";
}
g();
f();
g();
</syntaxhighlight>
this will output:
1
2
1
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
==Local variables in Ruby==
[[Ruby (programming language)|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 <code>$variable_name</code>, while a local variable has simply no $ sign in front of its name, like <code>variable_name</code> (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's <code>my</code>, not dynamically-scoped local variables like Perl's <code>local</code>. There is at least one library for Ruby that provides dynamically-scoped variables.
<ref>
Conrad Irwin.
"LSpace: Dynamic scope for Ruby".
December 2012
http://cirw.in/blog/lspace
Retrieved 2013-10-16.
</ref>
== See also ==
Line 56 ⟶ 63:
{{DEFAULTSORT:Local Variable}}
[[Category:Variable (computer
|