<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
the	O
funarg	B-Application
problem	I-Application
(	O
function	O
argument	O
problem	O
)	O
refers	O
to	O
the	O
difficulty	O
in	O
implementing	O
first-class	B-Application
functions	I-Application
(	O
functions	O
as	O
first-class	O
objects	O
)	O
in	O
programming	O
language	O
implementations	O
so	O
as	O
to	O
use	O
stack-based	B-General_Concept
memory	I-General_Concept
allocation	I-General_Concept
of	O
the	O
functions	O
.	O
</s>
<s>
A	O
standard	O
resolution	O
is	O
either	O
to	O
forbid	O
such	O
references	O
or	O
to	O
create	O
closures	B-Language
.	O
</s>
<s>
There	O
are	O
two	O
subtly	O
different	O
versions	O
of	O
the	O
funarg	B-Application
problem	I-Application
.	O
</s>
<s>
The	O
upwards	B-Application
funarg	I-Application
problem	I-Application
arises	O
from	O
returning	O
(	O
or	O
otherwise	O
transmitting	O
"	O
upwards	O
"	O
)	O
a	O
function	O
from	O
a	O
function	O
call	O
.	O
</s>
<s>
The	O
downwards	B-Application
funarg	I-Application
problem	I-Application
arises	O
from	O
passing	O
a	O
function	O
as	O
a	O
parameter	O
to	O
another	O
function	O
call	O
.	O
</s>
<s>
In	O
most	O
compiled	O
programs	O
,	O
this	O
local	O
state	O
is	O
stored	O
on	O
the	O
call	B-General_Concept
stack	I-General_Concept
in	O
a	O
data	O
structure	O
called	O
a	O
stack	O
frame	O
or	O
activation	O
record	O
.	O
</s>
<s>
The	O
upwards	B-Application
funarg	I-Application
problem	I-Application
arises	O
when	O
the	O
calling	O
function	O
refers	O
to	O
the	O
called/exited	O
function	O
's	O
state	O
after	O
that	O
function	O
has	O
returned	O
.	O
</s>
<s>
Therefore	O
,	O
the	O
stack	O
frame	O
containing	O
the	O
called	O
function	O
's	O
state	O
variables	O
must	O
not	O
be	O
deallocated	O
when	O
the	O
function	O
returns	O
,	O
violating	O
the	O
stack-based	B-General_Concept
function	I-General_Concept
call	I-General_Concept
paradigm	I-General_Concept
.	O
</s>
<s>
One	O
solution	O
to	O
the	O
upwards	B-Application
funarg	I-Application
problem	I-Application
is	O
to	O
simply	O
allocate	O
all	O
activation	O
records	O
from	O
the	O
heap	B-Application
instead	O
of	O
the	O
stack	O
and	O
rely	O
on	O
some	O
form	O
of	O
garbage	B-General_Concept
collection	I-General_Concept
or	O
reference	B-General_Concept
counting	I-General_Concept
to	O
deallocate	O
them	O
when	O
they	O
are	O
no	O
longer	O
needed	O
.	O
</s>
<s>
Managing	O
activation	O
records	O
on	O
the	O
heap	B-Application
has	O
historically	O
been	O
perceived	O
to	O
be	O
less	O
efficient	O
than	O
on	O
the	O
stack	O
(	O
although	O
this	O
is	O
partially	O
contradicted	O
)	O
and	O
has	O
been	O
perceived	O
to	O
impose	O
significant	O
implementation	O
complexity	O
.	O
</s>
<s>
Most	O
functions	O
in	O
typical	O
programs	O
(	O
less	O
so	O
for	O
programs	O
in	O
functional	B-Language
programming	I-Language
languages	I-Language
)	O
do	O
not	O
create	O
upwards	O
funargs	B-Application
,	O
adding	O
to	O
concerns	O
about	O
potential	O
overhead	O
associated	O
with	O
their	O
implementation	O
.	O
</s>
<s>
Furthermore	O
,	O
this	O
approach	O
is	O
genuinely	O
difficult	O
in	O
languages	O
that	O
do	O
not	O
support	O
garbage	B-General_Concept
collection	I-General_Concept
.	O
</s>
<s>
Some	O
efficiency-minded	O
compilers	O
employ	O
a	O
hybrid	O
approach	O
in	O
which	O
the	O
activation	O
records	O
for	O
a	O
function	O
are	O
allocated	O
from	O
the	O
stack	O
if	O
the	O
compiler	O
is	O
able	O
to	O
deduce	O
,	O
through	O
static	O
program	O
analysis	O
,	O
that	O
the	O
function	O
creates	O
no	O
upwards	O
funargs	B-Application
.	O
</s>
<s>
Otherwise	O
,	O
the	O
activation	O
records	O
are	O
allocated	O
from	O
the	O
heap	B-Application
.	O
</s>
<s>
Another	O
solution	O
is	O
to	O
simply	O
copy	O
the	O
value	O
of	O
the	O
variables	O
into	O
the	O
closure	B-Language
at	O
the	O
time	O
the	O
closure	B-Language
is	O
created	O
.	O
</s>
<s>
This	O
will	O
cause	O
a	O
different	O
behavior	O
in	O
the	O
case	O
of	O
mutable	O
variables	O
,	O
because	O
the	O
state	O
will	O
no	O
longer	O
be	O
shared	O
between	O
closures	B-Language
.	O
</s>
<s>
The	O
ML	B-Language
languages	I-Language
take	O
this	O
approach	O
,	O
since	O
variables	O
in	O
those	O
languages	O
are	O
bound	O
to	O
values	O
—	O
i.e.	O
</s>
<s>
Java	B-Language
also	O
takes	O
this	O
approach	O
with	O
respect	O
to	O
anonymous	O
classes	O
,	O
in	O
that	O
it	O
only	O
allows	O
one	O
to	O
refer	O
to	O
variables	O
in	O
the	O
enclosing	O
scope	O
that	O
are	O
final	B-Language
(	O
i.e.	O
</s>
<s>
PHP	B-Application
5.3	O
'	O
s	O
anonymous	O
functions	O
require	O
one	O
to	O
specify	O
which	O
variables	O
to	O
include	O
in	O
the	O
closure	B-Language
using	O
the	O
use	O
(	O
)	O
clause	O
;	O
if	O
the	O
variable	O
is	O
listed	O
by	O
reference	O
,	O
it	O
includes	O
a	O
reference	O
to	O
the	O
original	O
variable	O
;	O
otherwise	O
,	O
it	O
passes	O
the	O
value	O
.	O
</s>
<s>
In	O
Apple	O
's	O
Blocks	B-Language
anonymous	O
functions	O
,	O
captured	O
local	O
variables	O
are	O
by	O
default	O
captured	O
by	O
value	O
;	O
if	O
one	O
wants	O
to	O
share	O
the	O
state	O
between	O
closures	B-Language
or	O
between	O
the	O
closure	B-Language
and	O
the	O
outside	O
scope	O
,	O
the	O
variable	O
must	O
be	O
declared	O
with	O
the	O
__block	O
modifier	O
,	O
in	O
which	O
case	O
that	O
variable	O
is	O
allocated	O
on	O
the	O
heap	B-Application
.	O
</s>
<s>
The	O
following	O
Haskell-like	O
pseudocode	B-Language
defines	O
function	O
composition	O
:	O
</s>
<s>
λ	B-Language
is	O
the	O
operator	O
for	O
constructing	O
a	O
new	O
function	O
,	O
which	O
in	O
this	O
case	O
has	O
one	O
argument	O
,	O
x	O
,	O
and	O
returns	O
the	O
result	O
of	O
first	O
applying	O
g	O
to	O
x	O
,	O
then	O
applying	O
f	O
to	O
that	O
.	O
</s>
<s>
This	O
λ	B-Language
function	O
carries	O
the	O
functions	O
f	O
and	O
g	O
(	O
or	O
pointers	O
to	O
them	O
)	O
as	O
internal	O
state	O
.	O
</s>
<s>
When	O
the	O
internal	O
function	O
λx	B-Language
attempts	O
to	O
access	O
g	O
,	O
it	O
will	O
access	O
a	O
discarded	O
memory	O
area	O
.	O
</s>
<s>
A	O
downwards	B-Application
funarg	I-Application
may	O
also	O
refer	O
to	O
a	O
function	O
's	O
state	O
when	O
that	O
function	O
is	O
not	O
actually	O
executing	O
.	O
</s>
<s>
However	O
,	O
because	O
,	O
by	O
definition	O
,	O
the	O
existence	O
of	O
a	O
downwards	B-Application
funarg	I-Application
is	O
contained	O
in	O
the	O
execution	O
of	O
the	O
function	O
that	O
creates	O
it	O
,	O
the	O
stack	O
frame	O
for	O
the	O
function	O
can	O
usually	O
still	O
be	O
stored	O
on	O
the	O
stack	O
.	O
</s>
<s>
Nonetheless	O
,	O
the	O
existence	O
of	O
downwards	B-Application
funargs	I-Application
implies	O
a	O
tree	O
structure	O
of	O
closures	B-Language
and	O
stack	O
frames	O
that	O
can	O
complicate	O
human	O
and	O
machine	O
reasoning	O
about	O
the	O
program	O
state	O
.	O
</s>
<s>
The	O
downwards	B-Application
funarg	I-Application
problem	I-Application
complicates	O
the	O
efficient	O
compilation	O
of	O
tail	B-Language
calls	I-Language
and	O
code	O
written	O
in	O
continuation-passing	B-Application
style	I-Application
.	O
</s>
<s>
Historically	O
,	O
the	O
upwards	B-Application
funarg	I-Application
problem	I-Application
has	O
proven	O
to	O
be	O
the	O
more	O
difficult	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
Pascal	B-Application
programming	I-Application
language	I-Application
allows	O
functions	O
to	O
be	O
passed	O
as	O
arguments	O
but	O
not	O
returned	O
as	O
results	O
;	O
thus	O
implementations	O
of	O
Pascal	B-Application
are	O
required	O
to	O
address	O
the	O
downwards	B-Application
funarg	I-Application
problem	I-Application
but	O
not	O
the	O
upwards	O
one	O
.	O
</s>
<s>
The	O
Modula-2	B-Language
and	O
Oberon	B-Language
programming	I-Language
languages	I-Language
(	O
descendants	O
of	O
Pascal	B-Application
)	O
allow	O
functions	O
both	O
as	O
parameters	O
and	O
return	O
values	O
,	O
but	O
the	O
assigned	O
function	O
may	O
not	O
be	O
a	O
nested	O
function	O
.	O
</s>
<s>
The	O
C	B-Language
programming	I-Language
language	I-Language
historically	O
avoids	O
the	O
main	O
difficulty	O
of	O
the	O
funarg	B-Application
problem	I-Application
by	O
not	O
allowing	O
function	O
definitions	O
to	O
be	O
nested	O
;	O
because	O
the	O
environment	O
of	O
every	O
function	O
is	O
the	O
same	O
,	O
containing	O
just	O
the	O
statically	O
allocated	O
global	O
variables	O
and	O
functions	O
,	O
a	O
pointer	O
to	O
a	O
function	O
's	O
code	O
describes	O
the	O
function	O
completely	O
.	O
</s>
<s>
Apple	O
has	O
proposed	O
and	O
implemented	O
a	O
closure	B-Language
syntax	I-Language
for	I-Language
C	I-Language
that	O
solves	O
the	O
upwards	B-Application
funarg	I-Application
problem	I-Application
by	O
dynamically	O
moving	O
closures	B-Language
from	O
the	O
stack	O
to	O
the	O
heap	B-Application
as	O
necessary	O
.	O
</s>
<s>
The	O
Java	B-Language
programming	I-Language
language	I-Language
deals	O
with	O
it	O
by	O
requiring	O
that	O
context	O
used	O
by	O
nested	O
functions	O
in	O
anonymous	O
inner	O
and	O
local	O
classes	O
be	O
declared	O
final	B-Language
,	O
and	O
context	O
used	O
by	O
lambda	O
expressions	O
be	O
effectively	O
final	B-Language
.	O
</s>
<s>
C#	B-Application
and	O
D	B-Application
have	O
lambdas	O
(	O
closures	B-Language
)	O
that	O
encapsulate	O
a	O
function	O
pointer	O
and	O
related	O
variables	O
.	O
</s>
<s>
In	O
functional	B-Language
languages	I-Language
,	O
functions	O
are	O
first-class	O
values	O
that	O
can	O
be	O
passed	O
anywhere	O
.	O
</s>
<s>
Thus	O
,	O
implementations	O
of	O
Scheme	B-Language
or	O
Standard	B-Language
ML	I-Language
must	O
address	O
both	O
the	O
upwards	O
and	O
downwards	B-Application
funarg	I-Application
problems	I-Application
.	O
</s>
<s>
This	O
is	O
usually	O
accomplished	O
by	O
representing	O
function	O
values	O
as	O
heap-allocated	B-General_Concept
closures	B-Language
,	O
as	O
previously	O
described	O
.	O
</s>
<s>
The	O
OCaml	B-Language
compiler	O
employs	O
a	O
hybrid	O
technique	O
(	O
based	O
on	O
program	O
analysis	O
)	O
to	O
maximize	O
efficiency	O
.	O
</s>
