<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Local_variable</id>
	<title>Local variable - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Local_variable"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Local_variable&amp;action=history"/>
	<updated>2026-09-11T00:05:44Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki143/index.php?title=Local_variable&amp;diff=1272929&amp;oldid=prev</id>
		<title>imported&gt;Meno25: /* Static local variables */</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Local_variable&amp;diff=1272929&amp;oldid=prev"/>
		<updated>2023-11-25T15:53:43Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Static local variables&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Computer programming, a variable only usable in a portion of a program (the scope)}}&lt;br /&gt;
In [[computer science]], a &amp;#039;&amp;#039;&amp;#039;local variable&amp;#039;&amp;#039;&amp;#039; is a [[Variable (programming)|variable]] that is given &amp;#039;&amp;#039;local [[scope (programming)|scope]]&amp;#039;&amp;#039;.  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.&lt;br /&gt;
&lt;br /&gt;
==Scope==&lt;br /&gt;
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&amp;#039;s scope is a certain block, then its scope is the program text of the block definition: within that block&amp;#039;s text, the variable name exists, and is bound to the variable&amp;#039;s value, but outside that block&amp;#039;s text, the variable name does not exist. By contrast, in dynamic scoping (or dynamic scope), if a variable name&amp;#039;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]])&amp;#039;s &amp;quot;local&amp;quot; declaration.  Most other languages provide lexically scoped local variables.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Programming languages that employ &amp;#039;&amp;#039;[[call by value]]&amp;#039;&amp;#039; 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, &amp;#039;&amp;#039;[[call by reference]]&amp;#039;&amp;#039; and &amp;#039;&amp;#039;[[call by name]]&amp;#039;&amp;#039; semantics allow the parameters to act as aliases of the values passed as arguments, allowing the subroutine to modify variables outside its own scope.&lt;br /&gt;
&lt;br /&gt;
==Static local variables==&lt;br /&gt;
A special type of local variable, called a &amp;#039;&amp;#039;static local,&amp;#039;&amp;#039; 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 &amp;#039;&amp;#039;storage class&amp;#039;&amp;#039; keyword (e.g., &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
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,&amp;lt;ref&amp;gt;{{cite web|url= http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf |title=Current C standard }}&amp;amp;nbsp;{{small|(3.61&amp;amp;nbsp;MB)}} ({{As of|2009|lc=on}}). In particular, see section 6.2.4 “Storage durations of objects”, page 32.&amp;lt;/ref&amp;gt; but have [[function scope]] (not global scope), as with automatic local variables.&lt;br /&gt;
&lt;br /&gt;
This is distinct from other usages of the [[Static (keyword)|&amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; keyword]], which has several different meanings in various languages.&lt;br /&gt;
&lt;br /&gt;
==Local variables in Perl==&lt;br /&gt;
&lt;br /&gt;
[[Perl]] supports both dynamic and lexically-scoped local variables.  The keyword &amp;lt;code&amp;gt;local&amp;lt;/code&amp;gt; is used to define local dynamically-scoped variables, while &amp;lt;code&amp;gt;my&amp;lt;/code&amp;gt; is used for local lexically-scoped variables.  Since dynamic scoping is less common today, the Perl documentation warns that &amp;quot;&amp;lt;code&amp;gt;local&amp;lt;/code&amp;gt; isn&amp;#039;t what most people think of as “local”.&amp;quot;.&amp;lt;ref&amp;gt;[http://perldoc.perl.org/functions/local.html perldoc.perl.org: local]&amp;lt;/ref&amp;gt;  Instead, the &amp;lt;code&amp;gt;local&amp;lt;/code&amp;gt; keyword gives a temporary, [[scope (computer science)|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.&amp;lt;ref&amp;gt;[http://perldoc.perl.org/perlsub.html#Temporary-Values-via-local() perldoc.perl.org: perlsub: Temporary Values via &amp;lt;code&amp;gt;local()&amp;lt;/code&amp;gt;]&amp;lt;/ref&amp;gt;  To create lexically-scoped local variables, use the &amp;lt;code&amp;gt;my&amp;lt;/code&amp;gt; operator instead.&amp;lt;ref&amp;gt;[http://perldoc.perl.org/perlsub.html#Private-Variables-via-my() perldoc.perl.org: perlsub: Private Variables via &amp;lt;code&amp;gt;my()&amp;lt;/code&amp;gt;]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To understand how it works consider the following code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;perl&amp;quot;&amp;gt;&lt;br /&gt;
$a = 1;&lt;br /&gt;
sub f() {&lt;br /&gt;
    local $a;&lt;br /&gt;
    $a = 2;&lt;br /&gt;
    g();&lt;br /&gt;
}&lt;br /&gt;
sub g() {&lt;br /&gt;
    print &amp;quot;$a\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
g();&lt;br /&gt;
f();&lt;br /&gt;
g();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
this will output:&lt;br /&gt;
 1&lt;br /&gt;
 2&lt;br /&gt;
 1&lt;br /&gt;
&lt;br /&gt;
This happens since the global variable $a is modified to a new &amp;#039;&amp;#039;temporary&amp;#039;&amp;#039; (local) meaning inside {{code|f()}}, but the global value is restored upon leaving the scope of {{code|f()}}.&lt;br /&gt;
&lt;br /&gt;
Using &amp;lt;code&amp;gt;my&amp;lt;/code&amp;gt; in this case instead of &amp;lt;code&amp;gt;local&amp;lt;/code&amp;gt; would have printed 1 three times since in that case the &amp;lt;code&amp;gt;$a&amp;lt;/code&amp;gt; variable would be limited to the static scope of the function {{code|f()}} and not seen by {{code|g()}}.&amp;lt;br /&amp;gt;  Randal L. Schwartz and Tom Phoenix argue that the operator &amp;lt;code&amp;gt;local&amp;lt;/code&amp;gt; should have had a different name like &amp;lt;code&amp;gt;save&amp;lt;/code&amp;gt;.&amp;lt;ref&amp;gt;{{cite book|author=Randal L. Schwartz and Tom Phoenix|title=Learning Perl 3rd edition|at=paragraph 4.7|publisher=O&amp;#039;REILLY|date=2001-07-01|ISBN=0-596-00132-0|url=https://archive.org/details/learningperl00schw}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Local variables in Ruby==&lt;br /&gt;
[[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 &amp;lt;code&amp;gt;$variable_name&amp;lt;/code&amp;gt;, while a local variable has simply no $ sign in front of its name, like &amp;lt;code&amp;gt;variable_name&amp;lt;/code&amp;gt; (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&amp;#039;s &amp;lt;code&amp;gt;my&amp;lt;/code&amp;gt;, not dynamically-scoped local variables like Perl&amp;#039;s &amp;lt;code&amp;gt;local&amp;lt;/code&amp;gt;.  There is at least one library for Ruby that provides dynamically-scoped variables.&lt;br /&gt;
&amp;lt;ref&amp;gt;&lt;br /&gt;
Conrad Irwin.&lt;br /&gt;
&amp;quot;LSpace: Dynamic scope for Ruby&amp;quot;.&lt;br /&gt;
December 2012&lt;br /&gt;
http://cirw.in/blog/lspace&lt;br /&gt;
Retrieved 2013-10-16.&lt;br /&gt;
&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Global variable]]&lt;br /&gt;
* [[Non-local variable]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{DEFAULTSORT:Local Variable}}&lt;br /&gt;
[[Category:Variable (computer science)]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Meno25</name></author>
	</entry>
</feed>