<s>
In	O
computer	B-General_Concept
programming	I-General_Concept
,	O
the	O
scope	O
of	O
a	O
name	O
binding	O
(	O
an	O
association	O
of	O
a	O
name	O
to	O
an	O
entity	O
,	O
such	O
as	O
a	O
variable	O
)	O
is	O
the	O
part	O
of	O
a	O
program	B-Application
where	O
the	O
name	O
binding	O
is	O
valid	O
;	O
that	O
is	O
,	O
where	O
the	O
name	O
can	O
be	O
used	O
to	O
refer	O
to	O
the	O
entity	O
.	O
</s>
<s>
In	O
other	O
parts	O
of	O
the	O
program	B-Application
,	O
the	O
name	O
may	O
refer	O
to	O
a	O
different	O
entity	O
(	O
it	O
may	O
have	O
a	O
different	O
binding	O
)	O
,	O
or	O
to	O
nothing	O
at	O
all	O
(	O
it	O
may	O
be	O
unbound	O
)	O
.	O
</s>
<s>
The	O
scope	O
of	O
a	O
name	O
binding	O
is	O
also	O
known	O
as	O
the	O
visibility	B-Application
of	O
an	O
entity	O
,	O
particularly	O
in	O
older	O
or	O
more	O
technical	O
literature	O
—	O
this	O
is	O
from	O
the	O
perspective	O
of	O
the	O
referenced	O
entity	O
,	O
not	O
the	O
referencing	O
name	O
.	O
</s>
<s>
The	O
term	O
"	O
scope	O
"	O
is	O
also	O
used	O
to	O
refer	O
to	O
the	O
set	O
of	O
all	O
name	O
bindings	O
that	O
are	O
valid	O
within	O
a	O
part	O
of	O
a	O
program	B-Application
or	O
at	O
a	O
given	O
point	O
in	O
a	O
program	B-Application
,	O
which	O
is	O
more	O
correctly	O
referred	O
to	O
as	O
context	O
or	O
environment	O
.	O
</s>
<s>
Strictly	O
speaking	O
and	O
in	O
practice	O
for	O
most	O
programming	O
languages	O
,	O
"	O
part	O
of	O
a	O
program	B-Application
"	O
refers	O
to	O
a	O
portion	O
of	O
source	O
code	O
(	O
area	O
of	O
text	O
)	O
,	O
and	O
is	O
known	O
as	O
lexical	O
scope	O
.	O
</s>
<s>
In	O
some	O
languages	O
,	O
however	O
,	O
"	O
part	O
of	O
a	O
program	B-Application
"	O
refers	O
to	O
a	O
portion	O
of	O
run	B-Library
time	I-Library
(	O
time	O
period	O
during	O
execution	B-General_Concept
)	O
,	O
and	O
is	O
known	O
as	O
dynamic	O
scope	O
.	O
</s>
<s>
In	O
most	O
cases	O
,	O
name	B-Application
resolution	I-Application
based	O
on	O
lexical	O
scope	O
is	O
relatively	O
straightforward	O
to	O
use	O
and	O
to	O
implement	O
,	O
as	O
in	O
use	O
one	O
can	O
read	O
backwards	O
in	O
the	O
source	O
code	O
to	O
determine	O
to	O
which	O
entity	O
a	O
name	O
refers	O
,	O
and	O
in	O
implementation	B-Application
one	O
can	O
maintain	O
a	O
list	O
of	O
names	O
and	O
contexts	O
when	O
compiling	B-Language
or	O
interpreting	B-Application
a	O
program	B-Application
.	O
</s>
<s>
Difficulties	O
arise	O
in	O
name	O
masking	O
,	O
forward	O
declarations	O
,	O
and	O
hoisting	B-Language
,	O
while	O
considerably	O
subtler	O
ones	O
arise	O
with	O
non-local	O
variables	O
,	O
particularly	O
in	O
closures	B-Language
.	O
</s>
<s>
This	O
is	O
virtually	O
unchanged	O
from	O
its	O
1960	O
definition	O
in	O
the	O
specification	O
of	O
ALGOL	B-Language
60	I-Language
.	O
</s>
<s>
An	O
identifier	O
can	O
denote	O
an	O
object	O
;	O
a	O
function	O
;	O
a	O
tag	O
or	O
a	O
member	O
of	O
a	O
structure	O
,	O
union	O
,	O
or	O
enumeration	O
;	O
a	O
typedef	B-Language
name	O
;	O
a	O
label	O
name	O
;	O
a	O
macro	O
name	O
;	O
or	O
a	O
macro	O
parameter	O
.	O
</s>
<s>
The	O
same	O
identifier	O
can	O
denote	O
different	O
entities	O
at	O
different	O
points	O
in	O
the	O
program	B-Application
.	O
</s>
<s>
 [ ... ] 	O
For	O
each	O
different	O
entity	O
that	O
an	O
identifier	O
designates	O
,	O
the	O
identifier	O
is	O
visible	O
(	O
i.e.	O
,	O
can	O
be	O
used	O
)	O
only	O
within	O
a	O
region	O
of	O
program	B-Application
text	O
called	O
its	O
scope	O
.	O
</s>
<s>
A	O
fundamental	O
distinction	O
in	O
scope	O
is	O
what	O
"	O
part	O
of	O
a	O
program	B-Application
"	O
means	O
.	O
</s>
<s>
In	O
languages	O
with	O
lexical	O
scope	O
(	O
also	O
called	O
static	B-Language
scope	I-Language
)	O
,	O
name	B-Application
resolution	I-Application
depends	O
on	O
the	O
location	O
in	O
the	O
source	O
code	O
and	O
the	O
lexical	O
context	O
(	O
also	O
called	O
static	O
context	O
)	O
,	O
which	O
is	O
defined	O
by	O
where	O
the	O
named	O
variable	O
or	O
function	O
is	O
defined	O
.	O
</s>
<s>
In	O
contrast	O
,	O
in	O
languages	O
with	O
dynamic	O
scope	O
the	O
name	B-Application
resolution	I-Application
depends	O
upon	O
the	O
program	B-Application
state	O
when	O
the	O
name	O
is	O
encountered	O
which	O
is	O
determined	O
by	O
the	O
execution	B-General_Concept
context	O
(	O
also	O
called	O
runtime	B-Library
context	O
,	O
calling	O
context	O
or	O
dynamic	O
context	O
)	O
.	O
</s>
<s>
In	O
practice	O
,	O
with	O
lexical	O
scope	O
a	O
name	O
is	O
resolved	O
by	O
searching	O
the	O
local	O
lexical	O
context	O
,	O
then	O
if	O
that	O
fails	O
,	O
by	O
searching	O
the	O
outer	O
lexical	O
context	O
,	O
and	O
so	O
on	O
;	O
whereas	O
with	O
dynamic	O
scope	O
,	O
a	O
name	O
is	O
resolved	O
by	O
searching	O
the	O
local	O
execution	B-General_Concept
context	O
,	O
then	O
if	O
that	O
fails	O
,	O
by	O
searching	O
the	O
outer	O
execution	B-General_Concept
context	O
,	O
and	O
so	O
on	O
,	O
progressing	O
up	O
the	O
call	B-General_Concept
stack	I-General_Concept
.	O
</s>
<s>
Most	O
modern	O
languages	O
use	O
lexical	O
scope	O
for	O
variables	O
and	O
functions	O
,	O
though	O
dynamic	O
scope	O
is	O
used	O
in	O
some	O
languages	O
,	O
notably	O
some	O
dialects	O
of	O
Lisp	B-Language
,	O
some	O
"	O
scripting	O
"	O
languages	O
,	O
and	O
some	O
template	B-Language
languages	I-Language
.	O
</s>
<s>
Perl	B-Language
5	O
offers	O
both	O
lexical	O
and	O
dynamic	O
scope	O
.	O
</s>
<s>
Even	O
in	O
lexically	O
scoped	O
languages	O
,	O
scope	O
for	O
closures	B-Language
can	O
be	O
confusing	O
to	O
the	O
uninitiated	O
,	O
as	O
these	O
depend	O
on	O
the	O
lexical	O
context	O
where	O
the	O
closure	B-Language
is	O
defined	O
,	O
not	O
where	O
it	O
is	O
called	O
.	O
</s>
<s>
Lexical	O
resolution	O
can	O
be	O
determined	O
at	O
compile	B-Application
time	I-Application
,	O
and	O
is	O
also	O
known	O
as	O
early	O
binding	O
,	O
while	O
dynamic	O
resolution	O
can	O
in	O
general	O
only	O
be	O
determined	O
at	O
run	B-Library
time	I-Library
,	O
and	O
thus	O
is	O
known	O
as	O
late	O
binding	O
.	O
</s>
<s>
In	O
object-oriented	B-Language
programming	I-Language
,	O
dynamic	O
dispatch	O
selects	O
an	O
object	O
method	B-Language
at	B-Library
runtime	I-Library
,	O
though	O
whether	O
the	O
actual	O
name	O
binding	O
is	O
done	O
at	O
compile	B-Application
time	I-Application
or	O
run	B-Library
time	I-Library
depends	O
on	O
the	O
language	O
.	O
</s>
<s>
De	O
facto	O
dynamic	O
scope	O
is	O
common	O
in	O
macro	O
languages	O
,	O
which	O
do	O
not	O
directly	O
do	O
name	B-Application
resolution	I-Application
,	O
but	O
instead	O
expand	O
in	O
place	O
.	O
</s>
<s>
In	O
those	O
frameworks	O
the	O
scope	O
is	O
just	O
an	O
object	O
of	O
the	O
programming	O
language	O
that	O
they	O
use	O
(	O
JavaScript	B-Language
in	O
case	O
of	O
AngularJS	O
)	O
that	O
is	O
used	O
in	O
certain	O
ways	O
by	O
the	O
framework	O
to	O
emulate	O
dynamic	O
scope	O
in	O
a	O
language	O
that	O
uses	O
lexical	O
scope	O
for	O
its	O
variables	O
.	O
</s>
<s>
Those	O
AngularJS	O
scopes	O
can	O
themselves	O
be	O
in	O
context	O
or	O
not	O
in	O
context	O
(	O
using	O
the	O
usual	O
meaning	O
of	O
the	O
term	O
)	O
in	O
any	O
given	O
part	O
of	O
the	O
program	B-Application
,	O
following	O
the	O
usual	O
rules	O
of	O
variable	O
scope	O
of	O
the	O
language	O
like	O
any	O
other	O
object	O
,	O
and	O
using	O
their	O
own	O
inheritance	B-Language
and	O
transclusion	O
rules	O
.	O
</s>
<s>
Scope	O
is	O
an	O
important	O
component	O
of	O
name	B-Application
resolution	I-Application
,	O
which	O
is	O
in	O
turn	O
fundamental	O
to	O
language	B-Application
semantics	I-Application
.	O
</s>
<s>
Name	B-Application
resolution	I-Application
(	O
including	O
scope	O
)	O
varies	O
between	O
programming	O
languages	O
,	O
and	O
within	O
a	O
programming	O
language	O
,	O
varies	O
by	O
type	O
of	O
entity	O
;	O
the	O
rules	O
for	O
scope	O
are	O
called	O
scope	B-Language
rules	I-Language
(	O
or	O
scoping	B-Language
rules	I-Language
)	O
.	O
</s>
<s>
Together	O
with	O
namespaces	O
,	O
scope	B-Language
rules	I-Language
are	O
crucial	O
in	O
modular	B-Architecture
programming	I-Architecture
,	O
so	O
a	O
change	O
in	O
one	O
part	O
of	O
the	O
program	B-Application
does	O
not	O
break	O
an	O
unrelated	O
part	O
.	O
</s>
<s>
"	O
Scope	O
"	O
and	O
"	O
context	O
"	O
in	O
particular	O
are	O
frequently	O
confused	O
:	O
scope	O
is	O
a	O
property	O
of	O
a	O
name	O
binding	O
,	O
while	O
context	O
is	O
a	O
property	O
of	O
a	O
part	O
of	O
a	O
program	B-Application
,	O
that	O
is	O
either	O
a	O
portion	O
of	O
source	O
code	O
(	O
lexical	O
context	O
or	O
static	O
context	O
)	O
or	O
a	O
portion	O
of	O
run	B-Library
time	I-Library
(	O
execution	B-General_Concept
context	O
,	O
runtime	B-Library
context	O
,	O
calling	O
context	O
or	O
dynamic	O
context	O
)	O
.	O
</s>
<s>
Execution	B-General_Concept
context	O
consists	O
of	O
lexical	O
context	O
(	O
at	O
the	O
current	O
execution	B-General_Concept
point	O
)	O
plus	O
additional	O
runtime	B-Library
state	O
such	O
as	O
the	O
call	B-General_Concept
stack	I-General_Concept
.	O
</s>
<s>
Strictly	O
speaking	O
,	O
during	O
execution	B-General_Concept
a	O
program	B-Application
enters	O
and	O
exits	O
various	O
name	O
bindings	O
 '	O
scopes	O
,	O
and	O
at	O
a	O
point	O
in	O
execution	B-General_Concept
name	O
bindings	O
are	O
"	O
in	O
context	O
"	O
or	O
"	O
not	O
in	O
context	O
"	O
,	O
hence	O
name	O
bindings	O
"	O
come	O
into	O
context	O
"	O
or	O
"	O
go	B-Application
out	O
of	O
context	O
"	O
as	O
the	O
program	B-Application
execution	B-General_Concept
enters	O
or	O
exits	O
the	O
scope	O
.	O
</s>
<s>
Scope	O
is	O
a	O
source-code	O
level	O
concept	O
,	O
and	O
a	O
property	O
of	O
name	O
bindings	O
,	O
particularly	O
variable	O
or	O
function	O
name	O
bindings	O
—	O
names	O
in	O
the	O
source	O
code	O
are	O
references	O
to	O
entities	O
in	O
the	O
program	B-Application
—	O
and	O
is	O
part	O
of	O
the	O
behavior	O
of	O
a	O
compiler	B-Language
or	O
interpreter	B-Application
of	O
a	O
language	O
.	O
</s>
<s>
Using	O
the	O
value	O
of	O
a	O
variable	O
when	O
the	O
name	O
is	O
in	O
context	O
but	O
the	O
variable	O
is	O
uninitialized	O
is	O
analogous	O
to	O
dereferencing	O
(	O
accessing	O
the	O
value	O
of	O
)	O
a	O
wild	B-Error_Name
pointer	I-Error_Name
,	O
as	O
it	O
is	O
undefined	O
.	O
</s>
<s>
However	O
,	O
as	O
variables	O
are	O
not	O
destroyed	O
until	O
they	O
go	B-Application
out	O
of	O
context	O
,	O
the	O
analog	O
of	O
a	O
dangling	B-Error_Name
pointer	I-Error_Name
does	O
not	O
exist	O
.	O
</s>
<s>
For	O
entities	O
such	O
as	O
variables	O
,	O
scope	O
is	O
a	O
subset	O
of	O
lifetime	O
(	O
also	O
known	O
as	O
extent	O
)	O
—	O
a	O
name	O
can	O
only	O
refer	O
to	O
a	O
variable	O
that	O
exists	O
(	O
possibly	O
with	O
undefined	O
value	O
)	O
,	O
but	O
variables	O
that	O
exist	O
are	O
not	O
necessarily	O
visible	O
:	O
a	O
variable	O
may	O
exist	O
but	O
be	O
inaccessible	O
(	O
the	O
value	O
is	O
stored	O
but	O
not	O
referred	O
to	O
within	O
a	O
given	O
context	O
)	O
,	O
or	O
accessible	O
but	O
not	O
via	O
the	O
given	O
name	O
,	O
in	O
which	O
case	O
it	O
is	O
not	O
in	O
context	O
(	O
the	O
program	B-Application
is	O
"	O
out	O
of	O
the	O
scope	O
of	O
the	O
name	O
"	O
)	O
.	O
</s>
<s>
In	O
other	O
cases	O
"	O
lifetime	O
"	O
is	O
irrelevant	O
—	O
a	O
label	O
(	O
named	O
position	O
in	O
the	O
source	O
code	O
)	O
has	O
lifetime	O
identical	O
with	O
the	O
program	B-Application
(	O
for	O
statically	O
compiled	B-Language
languages	O
)	O
,	O
but	O
may	O
be	O
in	O
context	O
or	O
not	O
at	O
a	O
given	O
point	O
in	O
the	O
program	B-Application
,	O
and	O
likewise	O
for	O
static	B-General_Concept
variables	I-General_Concept
—	O
a	O
static	O
global	O
variable	O
is	O
in	O
context	O
for	O
the	O
entire	O
program	B-Application
,	O
while	O
a	O
static	O
local	O
variable	O
is	O
only	O
in	O
context	O
within	O
a	O
function	O
or	O
other	O
local	O
context	O
,	O
but	O
both	O
have	O
lifetime	O
of	O
the	O
entire	O
run	O
of	O
the	O
program	B-Application
.	O
</s>
<s>
Determining	O
which	O
entity	O
a	O
name	O
refers	O
to	O
is	O
known	O
as	O
name	B-Application
resolution	I-Application
or	O
name	O
binding	O
(	O
particularly	O
in	O
object-oriented	B-Language
programming	I-Language
)	O
,	O
and	O
varies	O
between	O
languages	O
.	O
</s>
<s>
Given	O
a	O
name	O
,	O
the	O
language	O
(	O
properly	O
,	O
the	O
compiler	B-Language
or	O
interpreter	B-Application
)	O
checks	O
all	O
entities	O
that	O
are	O
in	O
context	O
for	O
matches	O
;	O
in	O
case	O
of	O
ambiguity	O
(	O
two	O
entities	O
with	O
the	O
same	O
name	O
,	O
such	O
as	O
a	O
global	O
and	O
local	O
variable	O
with	O
the	O
same	O
name	O
)	O
,	O
the	O
name	B-Application
resolution	I-Application
rules	O
are	O
used	O
to	O
distinguish	O
them	O
.	O
</s>
<s>
Most	O
frequently	O
,	O
name	B-Application
resolution	I-Application
relies	O
on	O
an	O
"	O
inner-to-outer	O
context	O
"	O
rule	O
,	O
such	O
as	O
the	O
Python	O
LEGB	O
(	O
Local	O
,	O
Enclosing	O
,	O
Global	O
,	O
Built-in	O
)	O
rule	O
:	O
names	O
implicitly	O
resolves	O
to	O
the	O
narrowest	O
relevant	O
context	O
.	O
</s>
<s>
In	O
some	O
cases	O
name	B-Application
resolution	I-Application
can	O
be	O
explicitly	O
specified	O
,	O
such	O
as	O
by	O
the	O
global	O
and	O
nonlocal	O
keywords	O
in	O
Python	O
;	O
in	O
other	O
cases	O
the	O
default	O
rules	O
cannot	O
be	O
overridden	O
.	O
</s>
<s>
At	O
the	O
level	O
of	O
variables	O
,	O
this	O
is	O
known	O
as	O
variable	B-Language
shadowing	I-Language
.	O
</s>
<s>
Due	O
to	O
the	O
potential	O
for	O
logic	B-General_Concept
errors	I-General_Concept
from	O
masking	O
,	O
some	O
languages	O
disallow	O
or	O
discourage	O
masking	O
,	O
raising	O
an	O
error	O
or	O
warning	O
at	O
compile	B-Application
time	I-Application
or	O
run	B-Library
time	I-Library
.	O
</s>
<s>
Various	O
programming	O
languages	O
have	O
various	O
different	O
scope	B-Language
rules	I-Language
for	O
different	O
kinds	O
of	O
declarations	O
and	O
names	O
.	O
</s>
<s>
Such	O
scope	B-Language
rules	I-Language
have	O
a	O
large	O
effect	O
on	O
language	B-Application
semantics	I-Application
and	O
,	O
consequently	O
,	O
on	O
the	O
behavior	O
and	O
correctness	O
of	O
programs	O
.	O
</s>
<s>
In	O
languages	O
like	O
C++	B-Language
,	O
accessing	O
an	O
unbound	O
variable	O
does	O
not	O
have	O
well-defined	O
semantics	B-Application
and	O
may	O
result	O
in	O
undefined	B-Language
behavior	I-Language
,	O
similar	O
to	O
referring	O
to	O
a	O
dangling	B-Error_Name
pointer	I-Error_Name
;	O
and	O
declarations	O
or	O
names	O
used	O
outside	O
their	O
scope	O
will	O
generate	O
syntax	B-Application
errors	I-Application
.	O
</s>
<s>
Scope	O
can	O
vary	O
from	O
as	O
little	O
as	O
a	O
single	O
expression	O
to	O
as	O
much	O
as	O
the	O
entire	O
program	B-Application
,	O
with	O
many	O
possible	O
gradations	O
in	O
between	O
.	O
</s>
<s>
The	O
simplest	O
scope	O
rule	O
is	O
global	O
scope	O
—	O
all	O
entities	O
are	O
visible	O
throughout	O
the	O
entire	O
program	B-Application
.	O
</s>
<s>
The	O
most	O
basic	O
modular	B-Architecture
scope	O
rule	O
is	O
two-level	O
scope	O
,	O
with	O
a	O
global	O
scope	O
anywhere	O
in	O
the	O
program	B-Application
,	O
and	O
local	O
scope	O
within	O
a	O
function	O
.	O
</s>
<s>
More	O
sophisticated	O
modular	B-Architecture
programming	I-Architecture
allows	O
a	O
separate	O
module	B-Architecture
scope	O
,	O
where	O
names	O
are	O
visible	O
within	O
the	O
module	B-Architecture
(	O
private	O
to	O
the	O
module	B-Architecture
)	O
but	O
not	O
visible	O
outside	O
it	O
.	O
</s>
<s>
Within	O
a	O
function	O
,	O
some	O
languages	O
,	O
such	O
as	O
C	B-Language
,	O
allow	O
block	O
scope	O
to	O
restrict	O
scope	O
to	O
a	O
subset	O
of	O
a	O
function	O
;	O
others	O
,	O
notably	O
functional	B-Language
languages	I-Language
,	O
allow	O
expression	O
scope	O
,	O
to	O
restrict	O
scope	O
to	O
a	O
single	O
expression	O
.	O
</s>
<s>
Other	O
scopes	O
include	O
file	O
scope	O
(	O
notably	O
in	O
C	B-Language
)	O
which	O
behaves	O
similarly	O
to	O
module	B-Architecture
scope	O
,	O
and	O
block	O
scope	O
outside	O
of	O
functions	O
(	O
notably	O
in	O
Perl	B-Language
)	O
.	O
</s>
<s>
In	O
some	O
languages	O
,	O
such	O
as	O
C	B-Language
,	O
a	O
name	O
's	O
scope	O
begins	O
at	O
the	O
name	O
declaration	O
,	O
and	O
thus	O
different	O
names	O
declared	O
within	O
a	O
given	O
block	O
can	O
have	O
different	O
scopes	O
.	O
</s>
<s>
In	O
other	O
languages	O
,	O
such	O
as	O
Python	O
,	O
a	O
name	O
's	O
scope	O
begins	O
at	O
the	O
start	O
of	O
the	O
relevant	O
block	O
where	O
the	O
name	O
is	O
declared	O
(	O
such	O
as	O
the	O
start	O
of	O
a	O
function	O
)	O
,	O
regardless	O
of	O
where	O
it	O
is	O
defined	O
,	O
so	O
all	O
names	O
within	O
a	O
given	O
block	O
have	O
the	O
same	O
scope	O
.	O
</s>
<s>
In	O
JavaScript	B-Language
,	O
the	O
scope	O
of	O
a	O
name	O
declared	O
with	O
let	O
or	O
const	O
begins	O
at	O
the	O
name	O
declaration	O
,	O
and	O
the	O
scope	O
of	O
a	O
name	O
declared	O
with	O
var	O
begins	O
at	O
the	O
start	O
of	O
the	O
function	O
where	O
the	O
name	O
is	O
declared	O
,	O
which	O
is	O
known	O
as	O
variable	B-Language
hoisting	I-Language
.	O
</s>
<s>
Behavior	O
of	O
names	O
in	O
context	O
that	O
have	O
undefined	O
value	O
differs	O
:	O
in	O
Python	O
use	O
of	O
undefined	O
names	O
yields	O
a	O
runtime	B-Library
error	I-Library
,	O
while	O
in	O
JavaScript	B-Language
undefined	O
names	O
declared	O
with	O
var	O
are	O
usable	O
throughout	O
the	O
function	O
because	O
they	O
are	O
implicitly	O
bound	O
to	O
the	O
value	O
undefined	O
.	O
</s>
<s>
Expression	O
scope	O
is	O
available	O
in	O
many	O
languages	O
,	O
especially	O
functional	B-Language
languages	I-Language
which	O
offer	O
a	O
feature	O
called	O
let-expressions	O
allowing	O
a	O
declaration	O
's	O
scope	O
to	O
be	O
a	O
single	O
expression	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
Standard	B-Language
ML	I-Language
,	O
if	O
f( )	O
returns	O
12	O
,	O
then	O
let	O
val	O
x	O
=	O
f( )	O
in	O
x	O
*	O
x	O
end	O
is	O
an	O
expression	O
that	O
evaluates	O
to	O
144	O
,	O
using	O
a	O
temporary	O
variable	O
named	O
x	O
to	O
avoid	O
calling	O
f( )	O
twice	O
.	O
</s>
<s>
Some	O
languages	O
with	O
block	O
scope	O
approximate	O
this	O
functionality	O
by	O
offering	O
syntax	O
for	O
a	O
block	O
to	O
be	O
embedded	O
into	O
an	O
expression	O
;	O
for	O
example	O
,	O
the	O
aforementioned	O
Standard	B-Language
ML	I-Language
expression	O
could	O
be	O
written	O
in	O
Perl	B-Language
as	O
,	O
or	O
in	O
GNU	B-Application
C	I-Application
as	O
.	O
</s>
<s>
In	O
C	B-Language
,	O
variable	O
names	O
in	O
a	O
function	O
prototype	O
have	O
expression	O
scope	O
,	O
known	O
in	O
this	O
context	O
as	O
function	O
protocol	O
scope	O
.	O
</s>
<s>
This	O
began	O
with	O
ALGOL	B-Language
60	I-Language
,	O
where	O
"[e]very	O
declaration	O
...	O
is	O
valid	O
only	O
for	O
that	O
block	O
.	O
</s>
<s>
"	O
,	O
and	O
today	O
is	O
particularly	O
associated	O
with	O
languages	O
in	O
the	O
Pascal	B-Application
and	O
C	B-Language
families	O
and	O
traditions	O
.	O
</s>
<s>
Most	O
often	O
this	O
block	O
is	O
contained	O
within	O
a	O
function	O
,	O
thus	O
restricting	O
the	O
scope	O
to	O
a	O
part	O
of	O
a	O
function	O
,	O
but	O
in	O
some	O
cases	O
,	O
such	O
as	O
Perl	B-Language
,	O
the	O
block	O
may	O
not	O
be	O
within	O
a	O
function	O
.	O
</s>
<s>
A	O
representative	O
example	O
of	O
the	O
use	O
of	O
block	O
scope	O
is	O
the	O
C	B-Language
code	O
shown	O
here	O
,	O
where	O
two	O
variables	O
are	O
scoped	O
to	O
the	O
loop	O
:	O
the	O
loop	O
variable	O
n	O
,	O
which	O
is	O
initialized	O
once	O
and	O
incremented	O
on	O
each	O
iteration	O
of	O
the	O
loop	O
,	O
and	O
the	O
auxiliary	O
variable	O
n_squared	O
,	O
which	O
is	O
initialized	O
at	O
each	O
iteration	O
.	O
</s>
<s>
Blocks	O
are	O
primarily	O
used	O
for	O
control	O
flow	O
,	O
such	O
as	O
with	O
if	O
,	O
while	O
,	O
and	O
for	B-Language
loops	I-Language
,	O
and	O
in	O
these	O
cases	O
block	O
scope	O
means	O
the	O
scope	O
of	O
variable	O
depends	O
on	O
the	O
structure	O
of	O
a	O
function	O
's	O
flow	O
of	O
execution	B-General_Concept
.	O
</s>
<s>
A	O
subtlety	O
of	O
several	O
programming	O
languages	O
,	O
such	O
as	O
Algol	B-Language
68	I-Language
and	O
C	B-Language
(	O
demonstrated	O
in	O
this	O
example	O
and	O
standardized	O
since	O
C99	B-Language
)	O
,	O
is	O
that	O
block-scope	O
variables	O
can	O
be	O
declared	O
not	O
only	O
within	O
the	O
body	O
of	O
the	O
block	O
,	O
but	O
also	O
within	O
the	O
control	O
statement	O
,	O
if	O
any	O
.	O
</s>
<s>
This	O
is	O
primarily	O
used	O
in	O
for	B-Language
loops	I-Language
,	O
which	O
have	O
an	O
initialization	O
statement	O
separate	O
from	O
the	O
loop	O
condition	O
,	O
unlike	O
while	O
loops	O
,	O
and	O
is	O
a	O
common	O
idiom	O
.	O
</s>
<s>
Furthermore	O
,	O
some	O
descendants	O
of	O
C	B-Language
,	O
such	O
as	O
Java	B-Language
and	O
C#	B-Application
,	O
despite	O
having	O
support	O
for	O
block	O
scope	O
(	O
in	O
that	O
a	O
local	O
variable	O
can	O
be	O
made	O
to	O
go	B-Application
out	O
of	O
context	O
before	O
the	O
end	O
of	O
a	O
function	O
)	O
,	O
do	O
not	O
allow	O
one	O
local	O
variable	O
to	O
hide	O
another	O
.	O
</s>
<s>
In	O
such	O
languages	O
,	O
the	O
attempted	O
declaration	O
of	O
the	O
second	O
n	O
would	O
result	O
in	O
a	O
syntax	B-Application
error	I-Application
,	O
and	O
one	O
of	O
the	O
n	O
variables	O
would	O
have	O
to	O
be	O
renamed	O
.	O
</s>
<s>
In	O
Perl	B-Language
,	O
which	O
has	O
block	O
scope	O
,	O
this	O
instead	O
requires	O
declaring	O
the	O
variable	O
prior	O
to	O
the	O
block	O
:	O
</s>
<s>
while	O
in	O
Perl	B-Language
this	O
would	O
be	O
:	O
</s>
<s>
This	O
is	O
a	O
more	O
significant	O
issue	O
in	O
C	B-Language
,	O
notably	O
for	O
string	O
assignment	O
,	O
as	O
string	O
initialization	O
can	O
automatically	O
allocate	O
memory	O
,	O
while	O
string	O
assignment	O
to	O
an	O
already	O
initialized	O
variable	O
requires	O
allocating	O
memory	O
,	O
a	O
string	O
copy	O
,	O
and	O
checking	O
that	O
these	O
are	O
successful	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
the	O
Perl	B-Language
snippet	O
at	O
right	O
,	O
$counter	O
is	O
a	O
variable	O
name	O
with	O
block	O
scope	O
(	O
due	O
to	O
the	O
use	O
of	O
the	O
my	O
keyword	O
)	O
,	O
while	O
increment_counter	O
is	O
a	O
function	O
name	O
with	O
global	O
scope	O
.	O
</s>
<s>
This	O
idiom	O
allows	O
one	O
to	O
define	O
closures	B-Language
in	O
Perl	B-Language
.	O
</s>
<s>
In	O
most	O
cases	O
the	O
lifetime	O
of	O
the	O
variable	O
is	O
the	O
duration	O
of	O
the	O
function	O
call	O
—	O
it	O
is	O
an	O
automatic	B-General_Concept
variable	I-General_Concept
,	O
created	O
when	O
the	O
function	O
starts	O
(	O
or	O
the	O
variable	O
is	O
declared	O
)	O
,	O
destroyed	O
when	O
the	O
function	O
returns	O
—	O
while	O
the	O
scope	O
of	O
the	O
variable	O
is	O
within	O
the	O
function	O
,	O
though	O
the	O
meaning	O
of	O
"	O
within	O
"	O
depends	O
on	O
whether	O
scope	O
is	O
lexical	O
or	O
dynamic	O
.	O
</s>
<s>
However	O
,	O
some	O
languages	O
,	O
such	O
as	O
C	B-Language
,	O
also	O
provide	O
for	O
static	O
local	O
variables	O
,	O
where	O
the	O
lifetime	O
of	O
the	O
variable	O
is	O
the	O
entire	O
lifetime	O
of	O
the	O
program	B-Application
,	O
but	O
the	O
variable	O
is	O
only	O
in	O
context	O
when	O
inside	O
the	O
function	O
.	O
</s>
<s>
In	O
the	O
case	O
of	O
static	O
local	O
variables	O
,	O
the	O
variable	O
is	O
created	O
when	O
the	O
program	B-Application
initializes	O
,	O
and	O
destroyed	O
only	O
when	O
the	O
program	B-Application
terminates	O
,	O
as	O
with	O
a	O
static	O
global	O
variable	O
,	O
but	O
is	O
only	O
in	O
context	O
within	O
a	O
function	O
,	O
like	O
an	O
automatic	O
local	O
variable	O
.	O
</s>
<s>
By	O
contrast	O
,	O
in	O
dynamic	O
scope	O
,	O
the	O
scope	O
extends	O
to	O
the	O
execution	B-General_Concept
context	O
of	O
the	O
function	O
:	O
local	O
variables	O
stay	O
in	O
context	O
when	O
another	O
function	O
is	O
called	O
,	O
only	O
going	O
out	O
of	O
context	O
when	O
the	O
defining	O
function	O
ends	O
,	O
and	O
thus	O
local	O
variables	O
are	O
in	O
context	O
of	O
the	O
function	O
in	O
which	O
they	O
are	O
defined	O
and	O
all	O
called	O
functions	O
.	O
</s>
<s>
In	O
languages	O
with	O
lexical	O
scope	O
and	O
nested	B-General_Concept
functions	O
,	O
local	O
variables	O
are	O
in	O
context	O
for	O
nested	B-General_Concept
functions	O
,	O
since	O
these	O
are	O
within	O
the	O
same	O
lexical	O
context	O
,	O
but	O
not	O
for	O
other	O
functions	O
that	O
are	O
not	O
lexically	O
nested	B-General_Concept
.	O
</s>
<s>
A	O
local	O
variable	O
of	O
an	O
enclosing	O
function	O
is	O
known	O
as	O
a	O
non-local	O
variable	O
for	O
the	O
nested	B-General_Concept
function	O
.	O
</s>
<s>
Function	O
scope	O
is	O
also	O
applicable	O
to	O
anonymous	B-General_Concept
functions	I-General_Concept
.	O
</s>
<s>
These	O
two	O
n	O
variables	O
are	O
completely	O
separate	O
and	O
unrelated	O
,	O
despite	O
having	O
the	O
same	O
name	O
,	O
because	O
they	O
are	O
lexically	O
scoped	O
local	O
variables	O
with	O
function	O
scope	O
:	O
each	O
one	O
's	O
scope	O
is	O
its	O
own	O
,	O
lexically	O
separate	O
function	O
and	O
thus	O
,	O
they	O
do	O
n't	O
overlap	O
.	O
</s>
<s>
Function	O
scope	O
is	O
significantly	O
more	O
complicated	O
if	O
functions	O
are	O
first-class	B-Application
objects	O
and	O
can	O
be	O
created	O
locally	O
to	O
a	O
function	O
and	O
then	O
returned	O
.	O
</s>
<s>
In	O
this	O
case	O
any	O
variables	O
in	O
the	O
nested	B-General_Concept
function	O
that	O
are	O
not	O
local	O
to	O
it	O
(	O
unbound	O
variables	O
in	O
the	O
function	O
definition	O
,	O
that	O
resolve	O
to	O
variables	O
in	O
an	O
enclosing	O
context	O
)	O
create	O
a	O
closure	B-Language
,	O
as	O
not	O
only	O
the	O
function	O
itself	O
,	O
but	O
also	O
its	O
context	O
(	O
of	O
variables	O
)	O
must	O
be	O
returned	O
,	O
and	O
then	O
potentially	O
called	O
in	O
a	O
different	O
context	O
.	O
</s>
<s>
This	O
requires	O
significantly	O
more	O
support	O
from	O
the	O
compiler	B-Language
,	O
and	O
can	O
complicate	O
program	B-Application
analysis	O
.	O
</s>
<s>
File	O
scope	O
is	O
largely	O
particular	O
to	O
C	B-Language
(	O
and	O
C++	B-Language
)	O
,	O
where	O
scope	O
of	O
variables	O
and	O
functions	O
declared	O
at	O
the	O
top	O
level	O
of	O
a	O
file	O
(	O
not	O
within	O
any	O
function	O
)	O
is	O
for	O
the	O
entire	O
file	O
—	O
or	O
rather	O
for	O
C	B-Language
,	O
from	O
the	O
declaration	O
until	O
the	O
end	O
of	O
the	O
source	O
file	O
,	O
or	O
more	O
precisely	O
translation	B-Language
unit	I-Language
(	O
internal	O
linking	B-Application
)	O
.	O
</s>
<s>
This	O
can	O
be	O
seen	O
as	O
a	O
form	O
of	O
module	B-Architecture
scope	O
,	O
where	O
modules	B-Architecture
are	O
identified	O
with	O
files	O
,	O
and	O
in	O
more	O
modern	O
languages	O
is	O
replaced	O
by	O
an	O
explicit	O
module	B-Architecture
scope	O
.	O
</s>
<s>
In	O
the	O
C	B-Language
code	O
snippet	O
above	O
,	O
the	O
function	O
name	O
sum_of_squares	O
has	O
file	O
scope	O
.	O
</s>
<s>
The	O
scope	O
of	O
a	O
name	O
binding	O
is	O
a	O
module	B-Architecture
,	O
which	O
is	O
known	O
as	O
module	B-Architecture
scope	O
.	O
</s>
<s>
Module	B-Architecture
scope	O
is	O
available	O
in	O
modular	B-Architecture
programming	I-Architecture
languages	I-Architecture
where	O
modules	B-Architecture
(	O
which	O
may	O
span	O
various	O
files	O
)	O
are	O
the	O
basic	O
unit	O
of	O
a	O
complex	O
program	B-Application
,	O
as	O
they	O
allow	O
information	B-Application
hiding	I-Application
and	O
exposing	O
a	O
limited	O
interface	O
.	O
</s>
<s>
Module	B-Architecture
scope	O
was	O
pioneered	O
in	O
the	O
Modula	B-Language
family	O
of	O
languages	O
,	O
and	O
Python	O
(	O
which	O
was	O
influenced	O
by	O
Modula	B-Language
)	O
is	O
a	O
representative	O
contemporary	O
example	O
.	O
</s>
<s>
In	O
some	O
object-oriented	B-Language
programming	I-Language
languages	I-Language
that	O
lack	O
direct	O
support	O
for	O
modules	B-Architecture
,	O
such	O
as	O
C++	B-Language
,	O
a	O
similar	O
structure	O
is	O
instead	O
provided	O
by	O
the	O
class	O
hierarchy	O
,	O
where	O
classes	O
are	O
the	O
basic	O
unit	O
of	O
the	O
program	B-Application
,	O
and	O
a	O
class	O
can	O
have	O
private	O
methods	O
.	O
</s>
<s>
This	O
is	O
properly	O
understood	O
in	O
the	O
context	O
of	O
dynamic	O
dispatch	O
rather	O
than	O
name	B-Application
resolution	I-Application
and	O
scope	O
,	O
though	O
they	O
often	O
play	O
analogous	O
roles	O
.	O
</s>
<s>
In	O
some	O
cases	O
both	O
these	O
facilities	O
are	O
available	O
,	O
such	O
as	O
in	O
Python	O
,	O
which	O
has	O
both	O
modules	B-Architecture
and	O
classes	O
,	O
and	O
code	O
organization	O
(	O
as	O
a	O
module-level	O
function	O
or	O
a	O
conventionally	O
private	O
method	B-Language
)	O
is	O
a	O
choice	O
of	O
the	O
programmer	O
.	O
</s>
<s>
The	O
scope	O
of	O
a	O
name	O
binding	O
is	O
an	O
entire	O
program	B-Application
,	O
which	O
is	O
known	O
as	O
global	O
scope	O
.	O
</s>
<s>
Variable	O
names	O
with	O
global	O
scope	O
—	O
called	O
global	O
variables	O
—	O
are	O
frequently	O
considered	O
bad	O
practice	O
,	O
at	O
least	O
in	O
some	O
languages	O
,	O
due	O
to	O
the	O
possibility	O
of	O
name	O
collisions	O
and	O
unintentional	O
masking	O
,	O
together	O
with	O
poor	O
modularity	B-Architecture
,	O
and	O
function	O
scope	O
or	O
block	O
scope	O
are	O
considered	O
preferable	O
.	O
</s>
<s>
In	O
lexical	O
scope	O
(	O
or	O
lexical	O
scoping	B-Language
;	O
also	O
called	O
static	B-Language
scope	I-Language
or	O
static	O
scoping	B-Language
)	O
,	O
if	O
a	O
variable	O
name	O
's	O
scope	O
is	O
a	O
certain	O
function	O
,	O
then	O
its	O
scope	O
is	O
the	O
program	B-Application
text	O
of	O
the	O
function	O
definition	O
:	O
within	O
that	O
text	O
,	O
the	O
variable	O
name	O
exists	O
,	O
and	O
is	O
bound	O
to	O
the	O
variable	O
's	O
value	O
,	O
but	O
outside	O
that	O
text	O
,	O
the	O
variable	O
name	O
does	O
not	O
exist	O
.	O
</s>
<s>
By	O
contrast	O
,	O
in	O
dynamic	O
scope	O
(	O
or	O
dynamic	O
scoping	B-Language
)	O
,	O
if	O
a	O
variable	O
name	O
's	O
scope	O
is	O
a	O
certain	O
function	O
,	O
then	O
its	O
scope	O
is	O
the	O
time-period	O
during	O
which	O
the	O
function	O
is	O
executing	O
:	O
while	O
the	O
function	O
is	O
running	O
,	O
the	O
variable	O
name	O
exists	O
,	O
and	O
is	O
bound	O
to	O
its	O
value	O
,	O
but	O
after	O
the	O
function	O
returns	O
,	O
the	O
variable	O
name	O
does	O
not	O
exist	O
.	O
</s>
<s>
This	O
means	O
that	O
if	O
function	O
f	O
invokes	O
a	O
separately	O
defined	O
function	O
g	O
,	O
then	O
under	O
lexical	O
scope	O
,	O
function	O
g	O
does	O
not	O
have	O
access	O
to	O
f	O
's	O
local	O
variables	O
(	O
assuming	O
the	O
text	O
of	O
g	O
is	O
not	O
inside	O
the	O
text	O
of	O
f	O
)	O
,	O
while	O
under	O
dynamic	O
scope	O
,	O
function	O
g	O
does	O
have	O
access	O
to	O
f	O
's	O
local	O
variables	O
(	O
since	O
g	O
is	O
invoked	O
during	O
the	O
invocation	O
of	O
f	O
)	O
.	O
</s>
<s>
Consider	O
,	O
for	O
example	O
,	O
the	O
program	B-Application
on	O
the	O
right	O
.	O
</s>
<s>
So	O
,	O
what	O
exactly	O
does	O
this	O
program	B-Application
print	O
?	O
</s>
<s>
It	O
depends	O
on	O
the	O
scope	B-Language
rules	I-Language
.	O
</s>
<s>
If	O
the	O
language	O
of	O
this	O
program	B-Application
is	O
one	O
that	O
uses	O
lexical	O
scope	O
,	O
then	O
g	O
prints	O
and	O
modifies	O
the	O
global	O
variable	O
x	O
(	O
because	O
g	O
is	O
defined	O
outside	O
f	O
)	O
,	O
so	O
the	O
program	B-Application
prints	O
1	O
and	O
then	O
2	O
.	O
</s>
<s>
By	O
contrast	O
,	O
if	O
this	O
language	O
uses	O
dynamic	O
scope	O
,	O
then	O
g	O
prints	O
and	O
modifies	O
f	O
's	O
local	O
variable	O
x	O
(	O
because	O
g	O
is	O
called	O
from	O
within	O
f	O
)	O
,	O
so	O
the	O
program	B-Application
prints	O
3	O
and	O
then	O
1	O
.	O
</s>
<s>
(	O
As	O
it	O
happens	O
,	O
the	O
language	O
of	O
the	O
program	B-Application
is	O
Bash	B-Operating_System
,	O
which	O
uses	O
dynamic	O
scope	O
;	O
so	O
the	O
program	B-Application
prints	O
3	O
and	O
then	O
1	O
.	O
</s>
<s>
If	O
the	O
same	O
code	O
was	O
run	O
with	O
ksh93	B-Language
which	O
uses	O
lexical	O
scope	O
,	O
the	O
results	O
would	O
be	O
different	O
.	O
)	O
</s>
<s>
This	O
is	O
a	O
property	O
of	O
the	O
program	B-Application
text	O
and	O
is	O
made	O
independent	O
of	O
the	O
runtime	B-Library
call	B-General_Concept
stack	I-General_Concept
by	O
the	O
language	O
implementation	B-Application
.	O
</s>
<s>
Because	O
this	O
matching	O
only	O
requires	O
analysis	O
of	O
the	O
static	O
program	B-Application
text	O
,	O
this	O
type	O
of	O
scope	O
is	O
also	O
called	O
static	B-Language
scope	I-Language
.	O
</s>
<s>
Lexical	O
scope	O
is	O
standard	O
in	O
all	O
ALGOL-based	O
languages	O
such	O
as	O
Pascal	B-Application
,	O
Modula-2	B-Language
and	O
Ada	B-Language
as	O
well	O
as	O
in	O
modern	O
functional	B-Language
languages	I-Language
such	O
as	O
ML	B-Language
and	O
Haskell	B-Language
.	O
</s>
<s>
It	O
is	O
also	O
used	O
in	O
the	O
C	B-Language
language	I-Language
and	O
its	O
syntactic	O
and	O
semantic	O
relatives	O
,	O
although	O
with	O
different	O
kinds	O
of	O
limitations	O
.	O
</s>
<s>
Static	B-Language
scope	I-Language
allows	O
the	O
programmer	O
to	O
reason	O
about	O
object	O
references	O
such	O
as	O
parameters	O
,	O
variables	O
,	O
constants	O
,	O
types	O
,	O
functions	O
,	O
etc	O
.	O
</s>
<s>
This	O
makes	O
it	O
much	O
easier	O
to	O
make	O
modular	B-Architecture
code	O
and	O
reason	O
about	O
it	O
,	O
since	O
the	O
local	O
naming	O
structure	O
can	O
be	O
understood	O
in	O
isolation	O
.	O
</s>
<s>
In	O
contrast	O
,	O
dynamic	O
scope	O
forces	O
the	O
programmer	O
to	O
anticipate	O
all	O
possible	O
execution	B-General_Concept
contexts	O
in	O
which	O
the	O
module	B-Architecture
's	O
code	O
may	O
be	O
invoked	O
.	O
</s>
<s>
For	O
example	O
,	O
Pascal	B-Application
is	O
lexically	O
scoped	O
.	O
</s>
<s>
Consider	O
the	O
Pascal	B-Application
program	B-Application
fragment	O
at	O
right	O
.	O
</s>
<s>
The	O
char	O
variable	O
K	O
is	O
visible	O
only	O
in	O
the	O
main	O
program	B-Application
because	O
it	O
is	O
hidden	O
by	O
the	O
real	O
variable	O
K	O
visible	O
in	O
procedure	O
B	O
and	O
C	B-Language
only	O
.	O
</s>
<s>
Variable	O
L	O
is	O
also	O
visible	O
only	O
in	O
procedure	O
B	O
and	O
C	B-Language
but	O
it	O
does	O
not	O
hide	O
any	O
other	O
variable	O
.	O
</s>
<s>
Variable	O
M	O
is	O
only	O
visible	O
in	O
procedure	O
C	B-Language
and	O
therefore	O
not	O
accessible	O
either	O
from	O
procedure	O
B	O
or	O
the	O
main	O
program	B-Application
.	O
</s>
<s>
Also	O
,	O
procedure	O
C	B-Language
is	O
visible	O
only	O
in	O
procedure	O
B	O
and	O
can	O
therefore	O
not	O
be	O
called	O
from	O
the	O
main	O
program	B-Application
.	O
</s>
<s>
There	O
could	O
have	O
been	O
another	O
procedure	O
C	B-Language
declared	O
in	O
the	O
program	B-Application
outside	O
of	O
procedure	O
B	O
.	O
</s>
<s>
The	O
place	O
in	O
the	O
program	B-Application
where	O
"	O
C	B-Language
"	O
is	O
mentioned	O
then	O
determines	O
which	O
of	O
the	O
two	O
procedures	O
named	O
C	B-Language
it	O
represents	O
,	O
thus	O
precisely	O
analogous	O
with	O
the	O
scope	O
of	O
variables	O
.	O
</s>
<s>
Correct	O
implementation	B-Application
of	O
lexical	O
scope	O
in	O
languages	O
with	O
first-class	B-Application
nested	B-General_Concept
functions	O
is	O
not	O
trivial	O
,	O
as	O
it	O
requires	O
each	O
function	O
value	O
to	O
carry	O
with	O
it	O
a	O
record	O
of	O
the	O
values	O
of	O
the	O
variables	O
that	O
it	O
depends	O
on	O
(	O
the	O
pair	O
of	O
the	O
function	O
and	O
this	O
context	O
is	O
called	O
a	O
closure	B-Language
)	O
.	O
</s>
<s>
Depending	O
on	O
implementation	B-Application
and	O
computer	B-General_Concept
architecture	I-General_Concept
,	O
variable	O
lookup	B-Data_Structure
may	O
become	O
slightly	O
inefficient	O
when	O
very	O
deeply	O
lexically	O
nested	B-General_Concept
functions	O
are	O
used	O
,	O
although	O
there	O
are	O
well-known	O
techniques	O
to	O
mitigate	O
this	O
.	O
</s>
<s>
Also	O
,	O
for	O
nested	B-General_Concept
functions	O
that	O
only	O
refer	O
to	O
their	O
own	O
arguments	O
and	O
(	O
immediately	O
)	O
local	O
variables	O
,	O
all	O
relative	O
locations	O
can	O
be	O
known	O
at	O
compile	B-Application
time	I-Application
.	O
</s>
<s>
No	O
overhead	O
at	O
all	O
is	O
therefore	O
incurred	O
when	O
using	O
that	O
type	O
of	O
nested	B-General_Concept
function	O
.	O
</s>
<s>
The	O
same	O
applies	O
to	O
particular	O
parts	O
of	O
a	O
program	B-Application
where	O
nested	B-General_Concept
functions	O
are	O
not	O
used	O
,	O
and	O
,	O
naturally	O
,	O
to	O
programs	O
written	O
in	O
a	O
language	O
where	O
nested	B-General_Concept
functions	O
are	O
not	O
available	O
(	O
such	O
as	O
in	O
the	O
C	B-Language
language	I-Language
)	O
.	O
</s>
<s>
Lexical	O
scope	O
was	O
first	O
used	O
in	O
the	O
early	O
1960s	O
for	O
the	O
imperative	O
language	O
ALGOL	B-Language
60	I-Language
and	O
has	O
been	O
picked	O
up	O
in	O
most	O
other	O
imperative	O
languages	O
since	O
then	O
.	O
</s>
<s>
Languages	O
like	O
Pascal	B-Application
and	O
C	B-Language
have	O
always	O
had	O
lexical	O
scope	O
,	O
since	O
they	O
are	O
both	O
influenced	O
by	O
the	O
ideas	O
that	O
went	O
into	O
ALGOL	B-Language
60	I-Language
and	O
ALGOL	B-Language
68	I-Language
(	O
although	O
C	B-Language
did	O
not	O
include	O
lexically	O
nested	B-General_Concept
functions	O
)	O
.	O
</s>
<s>
Perl	B-Language
is	O
a	O
language	O
with	O
dynamic	O
scope	O
that	O
added	O
static	B-Language
scope	I-Language
afterwards	O
.	O
</s>
<s>
The	O
original	O
Lisp	B-Language
interpreter	B-Application
(	O
1960	O
)	O
used	O
dynamic	O
scope	O
.	O
</s>
<s>
Deep	O
binding	O
,	O
which	O
approximates	O
static	O
(	O
lexical	O
)	O
scope	O
,	O
was	O
introduced	O
around	O
1962	O
in	O
LISP	B-Language
1.5	I-Language
(	O
via	O
the	O
Funarg	B-Application
device	O
developed	O
by	O
Steve	O
Russell	O
,	O
working	O
under	O
John	O
McCarthy	O
)	O
.	O
</s>
<s>
All	O
early	O
Lisps	B-Language
used	O
dynamic	O
scope	O
,	O
when	O
based	O
on	O
interpreters	B-Application
.	O
</s>
<s>
In	O
1982	O
,	O
Guy	O
L	O
.	O
Steele	O
Jr.	O
and	O
the	O
Common	B-Language
LISP	I-Language
Group	O
publish	O
An	O
overview	O
of	O
Common	B-Language
LISP	I-Language
,	O
a	O
short	O
review	O
of	O
the	O
history	O
and	O
the	O
divergent	O
implementations	O
of	O
Lisp	B-Language
up	O
to	O
that	O
moment	O
and	O
a	O
review	O
of	O
the	O
features	O
that	O
a	O
Common	B-Language
Lisp	I-Language
implementation	B-Application
should	O
have	O
.	O
</s>
<s>
Most	O
LISP	B-Language
implementations	O
are	O
internally	O
inconsistent	O
in	O
that	O
by	O
default	O
the	O
interpreter	B-Application
and	O
compiler	B-Language
may	O
assign	O
different	O
semantics	B-Application
to	O
correct	O
programs	O
;	O
this	O
stems	O
primarily	O
from	O
the	O
fact	O
that	O
the	O
interpreter	B-Application
assumes	O
all	O
variables	O
to	O
be	O
dynamically	B-Language
scoped	I-Language
,	O
while	O
the	O
compiler	B-Language
assumes	O
all	O
variables	O
to	O
be	O
local	O
unless	O
forced	O
to	O
assume	O
otherwise	O
.	O
</s>
<s>
The	O
definition	O
of	O
Common	B-Language
LISP	I-Language
avoids	O
such	O
anomalies	O
by	O
explicitly	O
requiring	O
the	O
interpreter	B-Application
and	O
compiler	B-Language
to	O
impose	O
identical	O
semantics	B-Application
on	O
correct	O
programs	O
.	O
</s>
<s>
Implementations	O
of	O
Common	B-Language
LISP	I-Language
were	O
thus	O
required	O
to	O
have	O
lexical	O
scope	O
.	O
</s>
<s>
Again	O
,	O
from	O
An	O
overview	O
of	O
Common	B-Language
LISP	I-Language
:	O
</s>
<s>
In	O
addition	O
,	O
Common	B-Language
LISP	I-Language
offers	O
the	O
following	O
facilities	O
(	O
most	O
of	O
which	O
are	O
borrowed	O
from	O
MacLisp	B-Language
,	O
InterLisp	O
or	O
Lisp	B-Language
Machines	O
Lisp	B-Language
)	O
:	O
(	O
...	O
)	O
Fully	O
lexically	O
scoped	O
variables	O
.	O
</s>
<s>
The	O
so-called	O
"	O
FUNARG	B-Application
problem	I-Application
"	O
is	O
completely	O
solved	O
,	O
in	O
both	O
the	O
downward	O
and	O
upward	O
cases	O
.	O
</s>
<s>
By	O
the	O
same	O
year	O
in	O
which	O
An	O
overview	O
of	O
Common	B-Language
LISP	I-Language
was	O
published	O
(	O
1982	O
)	O
,	O
initial	O
designs	O
(	O
also	O
by	O
Guy	O
L	O
.	O
Steele	O
Jr.	O
)	O
of	O
a	O
compiled	B-Language
,	O
lexically	O
scoped	O
Lisp	B-Language
,	O
called	O
Scheme	B-Language
had	O
been	O
published	O
and	O
compiler	B-Language
implementations	O
were	O
being	O
attempted	O
.	O
</s>
<s>
At	O
that	O
time	O
,	O
lexical	O
scope	O
in	O
Lisp	B-Language
was	O
commonly	O
feared	O
to	O
be	O
inefficient	O
to	O
implement	O
.	O
</s>
<s>
All	O
serious	O
Lisps	B-Language
in	O
production	O
use	O
at	O
that	O
time	O
were	O
dynamically	B-Language
scoped	I-Language
.	O
</s>
<s>
The	O
term	O
"	O
lexical	O
scope	O
"	O
dates	O
at	O
least	O
to	O
1967	O
,	O
while	O
the	O
term	O
"	O
lexical	O
scoping	B-Language
"	O
dates	O
at	O
least	O
to	O
1970	O
,	O
where	O
it	O
was	O
used	O
in	O
Project	O
MAC	O
to	O
describe	O
the	O
scope	B-Language
rules	I-Language
of	O
the	O
Lisp	B-Language
dialect	O
MDL	B-Language
(	O
then	O
known	O
as	O
"	O
Muddle	O
"	O
)	O
.	O
</s>
<s>
With	O
dynamic	O
scope	O
,	O
a	O
name	O
refers	O
to	O
execution	B-General_Concept
context	O
.	O
</s>
<s>
In	O
technical	O
terms	O
,	O
this	O
means	O
that	O
each	O
name	O
has	O
a	O
global	O
stack	B-Application
of	O
bindings	O
.	O
</s>
<s>
Introducing	O
a	O
local	O
variable	O
with	O
name	O
x	O
pushes	O
a	O
binding	O
onto	O
the	O
global	O
x	O
stack	B-Application
(	O
which	O
may	O
have	O
been	O
empty	O
)	O
,	O
which	O
is	O
popped	O
off	O
when	O
the	O
control	O
flow	O
leaves	O
the	O
scope	O
.	O
</s>
<s>
Note	O
that	O
this	O
cannot	O
be	O
done	O
at	O
compile-time	B-Application
because	O
the	O
binding	O
stack	B-Application
only	O
exists	O
at	O
run-time	B-Library
,	O
which	O
is	O
why	O
this	O
type	O
of	O
scope	O
is	O
called	O
dynamic	O
scope	O
.	O
</s>
<s>
Generally	O
,	O
certain	O
blocks	O
are	O
defined	O
to	O
create	O
bindings	O
whose	O
lifetime	O
is	O
the	O
execution	B-Library
time	I-Library
of	O
the	O
block	O
;	O
this	O
adds	O
some	O
features	O
of	O
static	B-Language
scope	I-Language
to	O
the	O
dynamic	O
scope	O
process	O
.	O
</s>
<s>
This	O
can	O
be	O
beneficial	O
;	O
application	O
of	O
the	O
principle	B-Application
of	I-Application
least	I-Application
knowledge	I-Application
suggests	O
that	O
code	O
avoid	O
depending	O
on	O
the	O
reasons	O
for	O
(	O
or	O
circumstances	O
of	O
)	O
a	O
variable	O
's	O
value	O
,	O
but	O
simply	O
use	O
the	O
value	O
according	O
to	O
the	O
variable	O
's	O
definition	O
.	O
</s>
<s>
However	O
,	O
this	O
benefit	O
relies	O
on	O
careful	O
documentation	O
of	O
all	O
variables	O
used	O
this	O
way	O
as	O
well	O
as	O
on	O
careful	O
avoidance	O
of	O
assumptions	O
about	O
a	O
variable	O
's	O
behavior	O
,	O
and	O
does	O
not	O
provide	O
any	O
mechanism	O
to	O
detect	O
interference	O
between	O
different	O
parts	O
of	O
a	O
program	B-Application
.	O
</s>
<s>
Some	O
languages	O
,	O
like	O
Perl	B-Language
and	O
Common	B-Language
Lisp	I-Language
,	O
allow	O
the	O
programmer	O
to	O
choose	O
static	O
or	O
dynamic	O
scope	O
when	O
defining	O
or	O
redefining	O
a	O
variable	O
.	O
</s>
<s>
Examples	O
of	O
languages	O
that	O
use	O
dynamic	O
scope	O
include	O
Logo	B-Device
,	O
Emacs	B-Operating_System
Lisp	I-Operating_System
,	O
LaTeX	B-Application
and	O
the	O
shell	O
languages	O
bash	B-Operating_System
,	O
dash	B-Operating_System
,	O
and	O
PowerShell	B-Application
.	O
</s>
<s>
To	O
find	O
an	O
name	O
's	O
value	O
,	O
the	O
program	B-Application
could	O
traverse	O
the	O
runtime	B-General_Concept
stack	I-General_Concept
,	O
checking	O
each	O
activation	O
record	O
(	O
each	O
function	O
's	O
stack	B-Application
frame	O
)	O
for	O
a	O
value	O
for	O
the	O
name	O
.	O
</s>
<s>
In	O
practice	O
,	O
this	O
is	O
made	O
more	O
efficient	O
via	O
the	O
use	O
of	O
an	O
association	B-Data_Structure
list	I-Data_Structure
,	O
which	O
is	O
a	O
stack	B-Application
of	O
name/value	O
pairs	O
.	O
</s>
<s>
Pairs	O
are	O
pushed	O
onto	O
this	O
stack	B-Application
whenever	O
declarations	O
are	O
made	O
,	O
and	O
popped	O
whenever	O
variables	O
go	B-Application
out	O
of	O
context	O
.	O
</s>
<s>
Shallow	O
binding	O
is	O
an	O
alternative	O
strategy	O
that	O
is	O
considerably	O
faster	O
,	O
making	O
use	O
of	O
a	O
central	O
reference	O
table	O
,	O
which	O
associates	O
each	O
name	O
with	O
its	O
own	O
stack	B-Application
of	O
meanings	O
.	O
</s>
<s>
This	O
avoids	O
a	O
linear	O
search	O
during	O
run-time	B-Library
to	O
find	O
a	O
particular	O
name	O
,	O
but	O
care	O
should	O
be	O
taken	O
to	O
properly	O
maintain	O
this	O
table	O
.	O
</s>
<s>
Note	O
that	O
both	O
of	O
these	O
strategies	O
assume	O
a	O
last-in-first-out	O
(	O
LIFO	B-Application
)	O
ordering	O
to	O
bindings	O
for	O
any	O
one	O
variable	O
;	O
in	O
practice	O
all	O
bindings	O
are	O
so	O
ordered	O
.	O
</s>
<s>
An	O
even	O
simpler	O
implementation	B-Application
is	O
the	O
representation	O
of	O
dynamic	O
variables	O
with	O
simple	O
global	O
variables	O
.	O
</s>
<s>
The	O
local	O
binding	O
is	O
performed	O
by	O
saving	O
the	O
original	O
value	O
in	O
an	O
anonymous	O
location	O
on	O
the	O
stack	B-Application
that	O
is	O
invisible	O
to	O
the	O
program	B-Application
.	O
</s>
<s>
Early	O
implementations	O
of	O
Lisp	B-Language
used	O
this	O
obvious	O
strategy	O
for	O
implementing	O
local	O
variables	O
,	O
and	O
the	O
practice	O
survives	O
in	O
some	O
dialects	O
which	O
are	O
still	O
in	O
use	O
,	O
such	O
as	O
GNU	O
Emacs	B-Operating_System
Lisp	I-Operating_System
.	O
</s>
<s>
Lexical	O
scope	O
was	O
introduced	O
into	O
Lisp	B-Language
later	O
.	O
</s>
<s>
This	O
is	O
equivalent	O
to	O
the	O
above	O
shallow	O
binding	O
scheme	B-Language
,	O
except	O
that	O
the	O
central	O
reference	O
table	O
is	O
simply	O
the	O
global	O
variable	O
binding	O
context	O
,	O
in	O
which	O
the	O
current	O
meaning	O
of	O
the	O
variable	O
is	O
its	O
global	O
value	O
.	O
</s>
<s>
A	O
possible	O
implementation	B-Application
strategy	O
is	O
for	O
each	O
variable	O
to	O
have	O
a	O
thread-local	O
key	O
.	O
</s>
<s>
When	O
the	O
variable	O
is	O
accessed	O
,	O
the	O
thread-local	O
key	O
is	O
used	O
to	O
access	O
the	O
thread-local	O
memory	O
location	O
(	O
by	O
code	O
generated	O
by	O
the	O
compiler	B-Language
,	O
which	O
knows	O
which	O
variables	O
are	O
dynamic	O
and	O
which	O
are	O
lexical	O
)	O
.	O
</s>
<s>
When	O
a	O
variable	O
is	O
locally	O
bound	O
,	O
the	O
prior	O
value	O
is	O
stored	O
in	O
a	O
hidden	O
location	O
on	O
the	O
stack	B-Application
.	O
</s>
<s>
The	O
thread-local	O
storage	O
is	O
created	O
under	O
the	O
variable	O
's	O
key	O
,	O
and	O
the	O
new	O
value	O
is	O
stored	O
there	O
.	O
</s>
<s>
Further	O
nested	B-General_Concept
overrides	O
of	O
the	O
variable	O
within	O
that	O
thread	O
simply	O
save	O
and	O
restore	O
this	O
thread-local	O
location	O
.	O
</s>
<s>
When	O
the	O
initial	O
,	O
outermost	O
override	O
's	O
context	O
terminates	O
,	O
the	O
thread-local	O
key	O
is	O
deleted	O
,	O
exposing	O
the	O
global	O
version	O
of	O
the	O
variable	O
once	O
again	O
to	O
that	O
thread	O
.	O
</s>
<s>
With	O
referential	O
transparency	O
the	O
dynamic	O
scope	O
is	O
restricted	O
to	O
the	O
argument	O
stack	B-Application
of	O
the	O
current	O
function	O
only	O
,	O
and	O
coincides	O
with	O
the	O
lexical	O
scope	O
.	O
</s>
<s>
In	O
modern	O
languages	O
,	O
macro	O
expansion	O
in	O
a	O
preprocessor	B-General_Concept
is	O
a	O
key	O
example	O
of	O
de	O
facto	O
dynamic	O
scope	O
.	O
</s>
<s>
The	O
C	B-Language
preprocessor	I-Language
,	O
used	O
for	O
macro	O
expansion	O
,	O
has	O
de	O
facto	O
dynamic	O
scope	O
,	O
as	O
it	O
does	O
not	O
do	O
name	B-Application
resolution	I-Application
by	O
itself	O
and	O
it	O
is	O
independent	O
of	O
where	O
the	O
macro	O
is	O
defined	O
.	O
</s>
<s>
will	O
expand	O
to	O
add	O
a	O
to	O
the	O
passed	O
variable	O
,	O
with	O
this	O
name	O
only	O
later	O
resolved	O
by	O
the	O
compiler	B-Language
based	O
on	O
where	O
the	O
macro	O
ADD_A	O
is	O
"	O
called	O
"	O
(	O
properly	O
,	O
expanded	O
)	O
.	O
</s>
<s>
Properly	O
,	O
the	O
C	B-Language
preprocessor	I-Language
only	O
does	O
lexical	B-Application
analysis	I-Application
,	O
expanding	O
the	O
macro	O
during	O
the	O
tokenization	O
stage	O
,	O
but	O
not	O
parsing	O
into	O
a	O
syntax	O
tree	O
or	O
doing	O
name	B-Application
resolution	I-Application
.	O
</s>
<s>
Sometimes	O
this	O
restriction	O
is	O
inconvenient	O
;	O
when	O
many	O
different	O
things	O
need	O
to	O
be	O
accessible	O
throughout	O
a	O
program	B-Application
,	O
they	O
generally	O
all	O
need	O
names	O
with	O
global	O
scope	O
,	O
so	O
different	O
techniques	O
are	O
required	O
to	O
avoid	O
name	O
collisions	O
.	O
</s>
<s>
And	O
normally	O
these	O
groups	O
can	O
themselves	O
be	O
organized	O
into	O
groups	O
;	O
that	O
is	O
,	O
they	O
can	O
be	O
nested	B-General_Concept
.	O
</s>
<s>
Some	O
languages	O
have	O
mechanisms	O
,	O
such	O
as	O
namespaces	O
in	O
C++	B-Language
and	O
C#	B-Application
,	O
that	O
serve	O
almost	O
exclusively	O
to	O
enable	O
global	O
names	O
to	O
be	O
organized	O
into	O
groups	O
.	O
</s>
<s>
Other	O
languages	O
have	O
mechanisms	O
,	O
such	O
as	O
packages	O
in	O
Ada	B-Language
and	O
structures	O
in	O
Standard	B-Language
ML	I-Language
,	O
that	O
combine	O
this	O
with	O
the	O
additional	O
purpose	O
of	O
allowing	O
some	O
names	O
to	O
be	O
visible	O
only	O
to	O
other	O
members	O
of	O
their	O
group	O
.	O
</s>
<s>
And	O
object-oriented	B-Language
languages	I-Language
often	O
allow	O
classes	O
or	O
singleton	O
objects	O
to	O
fulfill	O
this	O
purpose	O
(	O
whether	O
or	O
not	O
they	O
also	O
have	O
a	O
mechanism	O
for	O
which	O
this	O
is	O
the	O
primary	O
purpose	O
)	O
.	O
</s>
<s>
Furthermore	O
,	O
languages	O
often	O
meld	O
these	O
approaches	O
;	O
for	O
example	O
,	O
Perl	B-Language
's	O
packages	O
are	O
largely	O
similar	O
to	O
C++'s	O
namespaces	O
,	O
but	O
optionally	O
double	O
as	O
classes	O
for	O
object-oriented	B-Language
programming	I-Language
;	O
and	O
Java	B-Language
organizes	O
its	O
variables	O
and	O
functions	O
into	O
classes	O
,	O
but	O
then	O
organizes	O
those	O
classes	O
into	O
Ada-like	O
packages	O
.	O
</s>
<s>
Scope	B-Language
rules	I-Language
for	O
representative	O
languages	O
follow	O
.	O
</s>
<s>
In	O
C	B-Language
,	O
scope	O
is	O
traditionally	O
known	O
as	O
linkage	B-Language
or	O
visibility	B-Application
,	O
particularly	O
for	O
variables	O
.	O
</s>
<s>
C	B-Language
is	O
a	O
lexically	O
scoped	O
language	O
with	O
global	O
scope	O
(	O
known	O
as	O
external	B-Language
linkage	I-Language
)	O
,	O
a	O
form	O
of	O
module	B-Architecture
scope	O
or	O
file	O
scope	O
(	O
known	O
as	O
internal	B-Language
linkage	I-Language
)	O
,	O
and	O
local	O
scope	O
(	O
within	O
a	O
function	O
)	O
;	O
within	O
a	O
function	O
scopes	O
can	O
further	O
be	O
nested	B-General_Concept
via	O
block	O
scope	O
.	O
</s>
<s>
However	O
,	O
standard	O
C	B-Language
does	O
not	O
support	O
nested	B-General_Concept
functions	O
.	O
</s>
<s>
The	O
lifetime	O
and	O
visibility	B-Application
of	O
a	O
variable	O
are	O
determined	O
by	O
its	O
storage	O
class	O
.	O
</s>
<s>
There	O
are	O
three	O
types	O
of	O
lifetimes	O
in	O
C	B-Language
:	O
static	O
(	O
program	B-Application
execution	B-General_Concept
)	O
,	O
automatic	O
(	O
block	O
execution	B-General_Concept
,	O
allocated	O
on	O
the	O
stack	B-Application
)	O
,	O
and	O
manual	O
(	O
allocated	O
on	O
the	O
heap	O
)	O
.	O
</s>
<s>
Only	O
static	O
and	O
automatic	O
are	O
supported	O
for	O
variables	O
and	O
handled	O
by	O
the	O
compiler	B-Language
,	O
while	O
manually	O
allocated	O
memory	O
must	O
be	O
tracked	O
manually	O
across	O
different	O
variables	O
.	O
</s>
<s>
There	O
are	O
three	O
levels	O
of	O
visibility	B-Application
in	O
C	B-Language
:	O
external	B-Language
linkage	I-Language
(	O
global	O
)	O
,	O
internal	B-Language
linkage	I-Language
(	O
roughly	O
file	O
)	O
,	O
and	O
block	O
scope	O
(	O
which	O
includes	O
functions	O
)	O
;	O
block	O
scopes	O
can	O
be	O
nested	B-General_Concept
,	O
and	O
different	O
levels	O
of	O
internal	B-Language
linkage	I-Language
is	O
possible	O
by	O
use	O
of	O
includes	O
.	O
</s>
<s>
Internal	B-Language
linkage	I-Language
in	O
C	B-Language
is	O
visibility	B-Application
at	O
the	O
translation	B-Language
unit	I-Language
level	O
,	O
namely	O
a	O
source	O
file	O
after	O
being	O
processed	O
by	O
the	O
C	B-Language
preprocessor	I-Language
,	O
notably	O
including	O
all	O
relevant	O
includes	O
.	O
</s>
<s>
C	B-Language
programs	I-Language
are	O
compiled	B-Language
as	O
separate	O
object	B-Application
files	I-Application
,	O
which	O
are	O
then	O
linked	O
into	O
an	O
executable	O
or	O
library	O
via	O
a	O
linker	B-Application
.	O
</s>
<s>
Thus	O
name	B-Application
resolution	I-Application
is	O
split	O
across	O
the	O
compiler	B-Language
,	O
which	O
resolves	O
names	O
within	O
a	O
translation	B-Language
unit	I-Language
(	O
more	O
loosely	O
,	O
"	O
compilation	B-Language
unit	I-Language
"	O
,	O
but	O
this	O
is	O
properly	O
a	O
different	O
concept	O
)	O
,	O
and	O
the	O
linker	B-Application
,	O
which	O
resolves	O
names	O
across	O
translation	B-Language
units	I-Language
;	O
see	O
linkage	B-Language
for	O
further	O
discussion	O
.	O
</s>
<s>
In	O
C	B-Language
,	O
variables	O
with	O
block	O
scope	O
enter	O
context	O
when	O
they	O
are	O
declared	O
(	O
not	O
at	O
the	O
top	O
of	O
the	O
block	O
)	O
,	O
go	B-Application
out	O
of	O
context	O
if	O
any	O
(	O
non-nested	O
)	O
function	O
is	O
called	O
within	O
the	O
block	O
,	O
come	O
back	O
into	O
context	O
when	O
the	O
function	O
returns	O
,	O
and	O
go	B-Application
out	O
of	O
context	O
at	O
the	O
end	O
of	O
the	O
block	O
.	O
</s>
<s>
In	O
the	O
case	O
of	O
automatic	O
local	O
variables	O
,	O
they	O
are	O
also	O
allocated	O
on	O
declaration	O
and	O
deallocated	O
at	O
the	O
end	O
of	O
the	O
block	O
,	O
while	O
for	O
static	O
local	O
variables	O
,	O
they	O
are	O
allocated	O
at	O
program	B-Application
initialization	O
and	O
deallocated	O
at	O
program	B-Application
termination	O
.	O
</s>
<s>
The	O
following	O
program	B-Application
demonstrates	O
a	O
variable	O
with	O
block	O
scope	O
coming	O
into	O
context	O
partway	O
through	O
the	O
block	O
,	O
then	O
exiting	O
context	O
(	O
and	O
in	O
fact	O
being	O
deallocated	O
)	O
when	O
the	O
block	O
ends	O
:	O
</s>
<s>
The	O
program	B-Application
outputs	O
:	O
</s>
<s>
There	O
are	O
other	O
levels	O
of	O
scope	O
in	O
C	B-Language
.	O
Variable	O
names	O
used	O
in	O
a	O
function	O
prototype	O
have	O
function	O
prototype	O
visibility	B-Application
,	O
and	O
exit	O
context	O
at	O
the	O
end	O
of	O
the	O
function	O
prototype	O
.	O
</s>
<s>
Since	O
the	O
name	O
is	O
not	O
used	O
,	O
this	O
is	O
not	O
useful	O
for	O
compilation	B-Language
,	O
but	O
may	O
be	O
useful	O
for	O
documentation	O
.	O
</s>
<s>
Modern	O
versions	O
allow	O
nested	B-General_Concept
lexical	O
scope	O
.	O
</s>
<s>
Swift	B-Application
has	O
a	O
similar	O
rule	O
for	O
scopes	O
with	O
C++	B-Language
,	O
but	O
contains	O
different	O
access	O
modifiers	O
.	O
</s>
<s>
Go	B-Application
is	O
lexically	O
scoped	O
using	O
blocks	O
.	O
</s>
<s>
Java	B-Language
is	O
lexically	O
scoped	O
.	O
</s>
<s>
A	O
Java	B-Language
class	O
can	O
contain	O
three	O
types	O
of	O
variables	O
:	O
</s>
<s>
Local	O
variables	O
are	O
defined	O
inside	O
a	O
method	B-Language
,	O
or	O
a	O
particular	O
block	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
loop	O
inside	O
a	O
method	B-Language
can	O
use	O
that	O
method	B-Language
's	O
local	O
variables	O
,	O
but	O
not	O
the	O
other	O
way	O
around	O
.	O
</s>
<s>
The	O
loop	O
's	O
variables	O
(	O
local	O
to	O
that	O
loop	O
)	O
are	O
destroyed	O
as	O
soon	O
as	O
the	O
loop	O
ends	O
.	O
</s>
<s>
Member	O
variables	O
also	O
called	O
fields	O
are	O
variables	O
declared	O
within	O
the	O
class	O
,	O
outside	O
of	O
any	O
method	B-Language
.	O
</s>
<s>
Parameters	O
are	O
variables	O
in	O
method	B-Language
declarations	O
.	O
</s>
<s>
JavaScript	B-Language
has	O
simple	O
scope	B-Language
rules	I-Language
,	O
but	O
variable	O
initialization	O
and	O
name	B-Application
resolution	I-Application
rules	O
can	O
cause	O
problems	O
,	O
and	O
the	O
widespread	O
use	O
of	O
closures	B-Language
for	O
callbacks	O
means	O
the	O
lexical	O
context	O
of	O
a	O
function	O
when	O
defined	O
(	O
which	O
is	O
used	O
for	O
name	B-Application
resolution	I-Application
)	O
can	O
be	O
very	O
different	O
from	O
the	O
lexical	O
context	O
when	O
it	O
is	O
called	O
(	O
which	O
is	O
irrelevant	O
for	O
name	B-Application
resolution	I-Application
)	O
.	O
</s>
<s>
JavaScript	B-Language
objects	O
have	O
name	B-Application
resolution	I-Application
for	O
properties	O
,	O
but	O
this	O
is	O
a	O
separate	O
topic	O
.	O
</s>
<s>
JavaScript	B-Language
has	O
lexical	O
scope	O
nested	B-General_Concept
at	O
the	O
function	O
level	O
,	O
with	O
the	O
global	O
context	O
being	O
the	O
outermost	O
context	O
.	O
</s>
<s>
Block	O
scope	O
with	O
the	O
let	O
and	O
const	O
keywords	O
is	O
standard	O
since	O
ECMAScript	B-Language
6	O
.	O
</s>
<s>
Block	O
scope	O
can	O
be	O
produced	O
by	O
wrapping	O
the	O
entire	O
block	O
in	O
a	O
function	O
and	O
then	O
executing	O
it	O
;	O
this	O
is	O
known	O
as	O
the	O
immediately-invoked	B-Language
function	I-Language
expression	I-Language
(	O
IIFE	B-Language
)	O
pattern	O
.	O
</s>
<s>
While	O
JavaScript	B-Language
scope	O
is	O
simple	O
—	O
lexical	O
,	O
function-level	O
—	O
the	O
associated	O
initialization	O
and	O
name	B-Application
resolution	I-Application
rules	O
are	O
a	O
cause	O
of	O
confusion	O
.	O
</s>
<s>
This	O
is	O
known	O
as	O
variable	B-Language
hoisting	I-Language
—	O
the	O
declaration	O
,	O
but	O
not	O
the	O
initialization	O
,	O
is	O
hoisted	O
to	O
the	O
top	O
of	O
the	O
function	O
.	O
</s>
<s>
Thirdly	O
,	O
accessing	O
variables	O
before	O
initialization	O
yields	O
undefined	O
,	O
rather	O
than	O
a	O
syntax	B-Application
error	I-Application
.	O
</s>
<s>
Further	O
,	O
as	O
functions	O
are	O
first-class	B-Application
objects	O
in	O
JavaScript	B-Language
and	O
are	O
frequently	O
assigned	O
as	O
callbacks	O
or	O
returned	O
from	O
functions	O
,	O
when	O
a	O
function	O
is	O
executed	O
,	O
the	O
name	B-Application
resolution	I-Application
depends	O
on	O
where	O
it	O
was	O
originally	O
defined	O
(	O
the	O
lexical	O
context	O
of	O
the	O
definition	O
)	O
,	O
not	O
the	O
lexical	O
context	O
or	O
execution	B-General_Concept
context	O
where	O
it	O
is	O
called	O
.	O
</s>
<s>
The	O
nested	B-General_Concept
scopes	O
of	O
a	O
particular	O
function	O
(	O
from	O
most	O
global	O
to	O
most	O
local	O
)	O
in	O
JavaScript	B-Language
,	O
particularly	O
of	O
a	O
closure	B-Language
,	O
used	O
as	O
a	O
callback	O
,	O
are	O
sometimes	O
referred	O
to	O
as	O
the	O
scope	O
chain	O
,	O
by	O
analogy	O
with	O
the	O
prototype	O
chain	O
of	O
an	O
object	O
.	O
</s>
<s>
Closures	B-Language
can	O
be	O
produced	O
in	O
JavaScript	B-Language
by	O
using	O
nested	B-General_Concept
functions	O
,	O
as	O
functions	O
are	O
first-class	B-Application
objects	O
.	O
</s>
<s>
Returning	O
a	O
nested	B-General_Concept
function	O
from	O
an	O
enclosing	O
function	O
includes	O
the	O
local	O
variables	O
of	O
the	O
enclosing	O
function	O
as	O
the	O
(	O
non-local	O
)	O
lexical	O
context	O
of	O
the	O
returned	O
function	O
,	O
yielding	O
a	O
closure	B-Language
.	O
</s>
<s>
Closures	B-Language
are	O
frequently	O
used	O
in	O
JavaScript	B-Language
,	O
due	O
to	O
being	O
used	O
for	O
callbacks	O
.	O
</s>
<s>
Indeed	O
,	O
any	O
hooking	O
of	O
a	O
function	O
in	O
the	O
local	O
context	O
as	O
a	O
callback	O
or	O
returning	O
it	O
from	O
a	O
function	O
creates	O
a	O
closure	B-Language
if	O
there	O
are	O
any	O
unbound	O
variables	O
in	O
the	O
function	O
body	O
(	O
with	O
the	O
context	O
of	O
the	O
closure	B-Language
based	O
on	O
the	O
nested	B-General_Concept
scopes	O
of	O
the	O
current	O
lexical	O
context	O
,	O
or	O
"	O
scope	O
chain	O
"	O
)	O
;	O
this	O
may	O
be	O
accidental	O
.	O
</s>
<s>
When	O
creating	O
a	O
callback	O
based	O
on	O
parameters	O
,	O
the	O
parameters	O
must	O
be	O
stored	O
in	O
a	O
closure	B-Language
,	O
otherwise	O
it	O
will	O
accidentally	O
create	O
a	O
closure	B-Language
that	O
refers	O
to	O
the	O
variables	O
in	O
the	O
enclosing	O
context	O
,	O
which	O
may	O
change	O
.	O
</s>
<s>
Name	B-Application
resolution	I-Application
of	O
properties	O
of	O
JavaScript	B-Language
objects	O
is	O
based	O
on	O
inheritance	B-Language
in	O
the	O
prototype	O
tree	O
—	O
a	O
path	O
to	O
the	O
root	O
in	O
the	O
tree	O
is	O
called	O
a	O
prototype	O
chain	O
—	O
and	O
is	O
separate	O
from	O
name	B-Application
resolution	I-Application
of	O
variables	O
and	O
functions	O
.	O
</s>
<s>
Lisp	B-Language
dialects	O
have	O
various	O
rules	O
for	O
scope	O
.	O
</s>
<s>
The	O
original	O
Lisp	B-Language
used	O
dynamic	O
scope	O
;	O
it	O
was	O
Scheme	B-Language
,	O
inspired	O
by	O
ALGOL	B-Language
,	O
that	O
introduced	O
static	O
(	O
lexical	O
)	O
scope	O
to	O
the	O
Lisp	B-Language
family	O
.	O
</s>
<s>
Maclisp	B-Language
used	O
dynamic	O
scope	O
by	O
default	O
in	O
the	O
interpreter	B-Application
and	O
lexical	O
scope	O
by	O
default	O
in	O
compiled	B-Language
code	O
,	O
though	O
compiled	B-Language
code	O
could	O
access	O
dynamic	O
bindings	O
by	O
use	O
of	O
SPECIAL	O
declarations	O
for	O
particular	O
variables	O
.	O
</s>
<s>
However	O
,	O
Maclisp	B-Language
treated	O
lexical	O
binding	O
more	O
as	O
an	O
optimization	O
than	O
one	O
would	O
expect	O
in	O
modern	O
languages	O
,	O
and	O
it	O
did	O
not	O
come	O
with	O
the	O
closure	B-Language
feature	O
one	O
might	O
expect	O
of	O
lexical	O
scope	O
in	O
modern	O
Lisps	B-Language
.	O
</s>
<s>
Common	B-Language
Lisp	I-Language
adopted	O
lexical	O
scope	O
from	O
Scheme	B-Language
,	O
as	O
did	O
Clojure	B-Language
.	O
</s>
<s>
ISLISP	B-Language
has	O
lexical	O
scope	O
for	O
ordinary	O
variables	O
.	O
</s>
<s>
Some	O
other	O
dialects	O
of	O
Lisp	B-Language
,	O
like	O
Emacs	B-Operating_System
Lisp	I-Operating_System
,	O
still	O
use	O
dynamic	O
scope	O
by	O
default	O
.	O
</s>
<s>
Emacs	B-Operating_System
Lisp	I-Operating_System
now	O
has	O
lexical	O
scope	O
available	O
on	O
a	O
per-buffer	O
basis	O
.	O
</s>
<s>
For	O
variables	O
,	O
Python	O
has	O
function	O
scope	O
,	O
module	B-Architecture
scope	O
,	O
and	O
global	O
scope	O
.	O
</s>
<s>
Names	O
enter	O
context	O
at	O
the	O
start	O
of	O
a	O
scope	O
(	O
function	O
,	O
module	B-Architecture
,	O
or	O
global	O
scope	O
)	O
,	O
and	O
exit	O
context	O
when	O
a	O
non-nested	O
function	O
is	O
called	O
or	O
the	O
scope	O
ends	O
.	O
</s>
<s>
If	O
a	O
name	O
is	O
used	O
prior	O
to	O
variable	O
initialization	O
,	O
this	O
raises	O
a	O
runtime	B-Library
exception	O
.	O
</s>
<s>
If	O
a	O
variable	O
is	O
simply	O
accessed	O
(	O
not	O
assigned	O
to	O
)	O
,	O
name	B-Application
resolution	I-Application
follows	O
the	O
LEGB	O
(	O
Local	O
,	O
Enclosing	O
,	O
Global	O
,	O
Built-in	O
)	O
rule	O
which	O
resolves	O
names	O
to	O
the	O
narrowest	O
relevant	O
context	O
.	O
</s>
<s>
However	O
,	O
if	O
a	O
variable	O
is	O
assigned	O
to	O
,	O
it	O
defaults	O
to	O
declaring	O
a	O
variable	O
whose	O
scope	O
starts	O
at	O
the	O
start	O
of	O
the	O
level	O
(	O
function	O
,	O
module	B-Architecture
,	O
or	O
global	O
)	O
,	O
not	O
at	O
the	O
assignment	O
.	O
</s>
<s>
This	O
differs	O
from	O
C	B-Language
,	O
where	O
the	O
scope	O
of	O
the	O
local	O
variable	O
start	O
at	O
its	O
declaration	O
.	O
</s>
<s>
The	O
default	O
name	B-Application
resolution	I-Application
rules	O
can	O
be	O
overridden	O
with	O
the	O
global	O
or	O
nonlocal	O
(	O
in	O
Python	O
3	O
)	O
keywords	O
.	O
</s>
<s>
global	O
can	O
also	O
be	O
used	O
for	O
nested	B-General_Concept
functions	O
.	O
</s>
<s>
For	O
nested	B-General_Concept
functions	O
,	O
there	O
is	O
also	O
the	O
nonlocal	O
declaration	O
,	O
for	O
assigning	O
to	O
a	O
nonlocal	O
variable	O
,	O
similar	O
to	O
using	O
global	O
in	O
an	O
unnested	O
function	O
:	O
</s>
<s>
R	B-Language
is	O
a	O
lexically	O
scoped	O
language	O
,	O
unlike	O
other	O
implementations	O
of	O
S	B-Language
where	O
the	O
values	O
of	O
free	O
variables	O
are	O
determined	O
by	O
a	O
set	O
of	O
global	O
variables	O
,	O
while	O
in	O
R	B-Language
they	O
are	O
determined	O
by	O
the	O
context	O
in	O
which	O
the	O
function	O
was	O
created	O
.	O
</s>
<s>
Although	O
R	B-Language
has	O
lexical	O
scope	O
by	O
default	O
,	O
function	O
scopes	O
can	O
be	O
changed	O
:	O
</s>
