Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Scope (computer science)
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Lexical scope vs. dynamic scope {{anchor|Lexical scoping and dynamic scoping}} == The use of local variables β of variable names with limited scope, that only exist within a specific function β helps avoid the risk of a name collision between two identically named variables. However, there are two very different approaches to answering this question: What does it mean to be "within" a function? In '''lexical scope''' (or '''lexical scoping'''; also called '''static scope''' or '''static scoping'''), if a variable name's scope is a certain function, then its scope is the program text of the function definition: within that text, the variable name exists, and is bound to the variable's value, but outside that text, the variable name does not exist. By contrast, in '''dynamic scope''' (or '''dynamic scoping'''), if a variable name's scope is a certain function, then its scope is the time-period during which the function is executing: while the function is running, the variable name exists, and is bound to its value, but after the function returns, the variable name does not exist. This means that if function <code>f</code> invokes a separately defined function <code>g</code>, then under lexical scope, function <code>g</code> does ''not'' have access to <code>f</code>'s local variables (assuming the text of <code>g</code> is not inside the text of <code>f</code>), while under dynamic scope, function <code>g</code> ''does'' have access to <code>f</code>'s local variables (since <code>g</code> is invoked during the invocation of <code>f</code>). <syntaxhighlight lang="console" style="float:right;margin-left:1em"> $ # bash language $ x=1 $ function g() { echo $x ; x=2 ; } $ function f() { local x=3 ; g ; } $ f # does this print 1, or 3? 3 $ echo $x # does this print 1, or 2? 1 </syntaxhighlight> Consider, for example, the program on the right. The first line, <syntaxhighlight lang="bash" inline>x=1</syntaxhighlight>, creates a global variable <code>x</code> and initializes it to <code>1</code>. The second line, <syntaxhighlight lang="bash" inline>function g() { echo $x ; x=2 ; }</syntaxhighlight>, defines a function <code>g</code> that prints out ("echoes") the current value of <code>x</code>, and then sets <code>x</code> to <code>2</code> (overwriting the previous value). The third line, <syntaxhighlight lang="bash" inline>function f() { local x=3 ; g ; }</syntaxhighlight> defines a function <code>f</code> that creates a local variable <code>x</code> (hiding the identically named global variable) and initializes it to <code>3</code>, and then calls <code>g</code>. The fourth line, <syntaxhighlight lang="bash" inline>f</syntaxhighlight>, calls <code>f</code>. The fifth line, <syntaxhighlight lang="bash" inline>echo $x</syntaxhighlight>, prints out the current value of <code>x</code>. So, what exactly does this program print? It depends on the scope rules. If the language of this program is one that uses lexical scope, then <code>g</code> prints and modifies the global variable <code>x</code> (because <code>g</code> is defined outside <code>f</code>), so the program prints <code>1</code> and then <code>2</code>. By contrast, if this language uses dynamic scope, then <code>g</code> prints and modifies <code>f</code>'s local variable <code>x</code> (because <code>g</code> is called from within <code>f</code>), so the program prints <code>3</code> and then <code>1</code>. (As it happens, the language of the program is [[Bash (Unix shell)|Bash]], which uses dynamic scope; so the program prints <code>3</code> and then <code>1</code>. If the same code was run with [[KornShell|ksh93]] which uses lexical scope, the results would be different.){{Clear}}
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Scope (computer science)
(section)
Add topic