<s>
A	O
function	B-Language
pointer	I-Language
,	O
also	O
called	O
a	O
subroutine	B-Language
pointer	I-Language
or	O
procedure	B-Language
pointer	I-Language
,	O
is	O
a	O
pointer	O
that	O
points	O
to	O
a	O
function	O
.	O
</s>
<s>
As	O
opposed	O
to	O
referencing	O
a	O
data	O
value	O
,	O
a	O
function	B-Language
pointer	I-Language
points	O
to	O
executable	O
code	O
within	O
memory	O
.	O
</s>
<s>
Dereferencing	O
the	O
function	B-Language
pointer	I-Language
yields	O
the	O
referenced	O
function	O
,	O
which	O
can	O
be	O
invoked	O
and	O
passed	O
arguments	O
just	O
as	O
in	O
a	O
normal	O
function	O
call	O
.	O
</s>
<s>
Such	O
an	O
invocation	O
is	O
also	O
known	O
as	O
an	O
"	O
indirect	O
"	O
call	O
,	O
because	O
the	O
function	O
is	O
being	O
invoked	O
indirectly	O
through	O
a	O
variable	O
instead	O
of	O
directly	O
through	O
a	O
fixed	O
identifier	O
or	O
address	B-General_Concept
.	O
</s>
<s>
Function	B-Language
pointers	I-Language
can	O
be	O
used	O
to	O
simplify	O
code	O
by	O
providing	O
a	O
simple	O
way	O
to	O
select	O
a	O
function	O
to	O
execute	O
based	O
on	O
run-time	O
values	O
.	O
</s>
<s>
Function	B-Language
pointers	I-Language
are	O
supported	O
by	O
third-generation	O
programming	O
languages	O
(	O
such	O
as	O
PL/I	B-Language
,	O
COBOL	B-Application
,	O
Fortran	B-Application
,	O
dBASE	O
dBL	O
,	O
and	O
C	B-Language
)	O
and	O
object-oriented	B-Language
programming	I-Language
languages	I-Language
(	O
such	O
as	O
C++	B-Language
,	O
C#	B-Application
,	O
and	O
D	B-Application
)	O
.	O
</s>
<s>
The	O
simplest	O
implementation	O
of	O
a	O
function	O
(	O
or	O
subroutine	O
)	O
pointer	O
is	O
as	O
a	O
variable	O
containing	O
the	O
address	B-General_Concept
of	O
the	O
function	O
within	O
executable	O
memory	O
.	O
</s>
<s>
Older	O
third-generation	O
languages	O
such	O
as	O
PL/I	B-Language
and	O
COBOL	B-Application
,	O
as	O
well	O
as	O
more	O
modern	O
languages	O
such	O
as	O
Pascal	B-Application
and	O
C	B-Language
generally	O
implement	O
function	B-Language
pointers	I-Language
in	O
this	B-Application
manner	O
.	O
</s>
<s>
The	O
following	O
C	B-Language
program	I-Language
illustrates	O
the	O
use	O
of	O
two	O
function	B-Language
pointers	I-Language
:	O
</s>
<s>
func2	O
takes	O
a	O
pointer	O
to	O
a	O
constant	O
character	O
array	O
as	O
well	O
as	O
an	O
integer	O
and	O
returns	O
a	O
pointer	O
to	O
a	O
character	O
,	O
and	O
is	O
assigned	O
to	O
a	O
C	B-Language
string	I-Language
handling	I-Language
function	O
which	O
returns	O
a	O
pointer	O
to	O
the	O
first	O
occurrence	O
of	O
a	O
given	O
character	O
in	O
a	O
character	O
array	O
.	O
</s>
<s>
The	O
next	O
program	O
uses	O
a	O
function	B-Language
pointer	I-Language
to	O
invoke	O
one	O
of	O
two	O
functions	O
(	O
sin	O
or	O
cos	O
)	O
indirectly	O
from	O
another	O
function	O
(	O
compute_sum	O
,	O
computing	O
an	O
approximation	O
of	O
the	O
function	O
's	O
Riemann	O
integration	O
)	O
.	O
</s>
<s>
The	O
program	O
operates	O
by	O
having	O
function	O
main	O
call	O
function	O
compute_sum	O
twice	O
,	O
passing	O
it	O
a	O
pointer	O
to	O
the	O
library	O
function	O
sin	O
the	O
first	O
time	O
,	O
and	O
a	O
pointer	B-Language
to	I-Language
function	I-Language
cos	O
the	O
second	O
time	O
.	O
</s>
<s>
Function	O
compute_sum	O
in	O
turn	O
invokes	O
one	O
of	O
the	O
two	O
functions	O
indirectly	O
by	O
dereferencing	O
its	O
function	B-Language
pointer	I-Language
argument	O
funcp	O
multiple	O
times	O
,	O
adding	O
together	O
the	O
values	O
that	O
the	O
invoked	O
function	O
returns	O
and	O
returning	O
the	O
resulting	O
sum	O
.	O
</s>
<s>
Functors	B-Language
,	O
or	O
function	B-Language
objects	I-Language
,	O
are	O
similar	O
to	O
function	B-Language
pointers	I-Language
,	O
and	O
can	O
be	O
used	O
in	O
similar	O
ways	O
.	O
</s>
<s>
A	O
functor	B-Language
is	O
an	O
object	O
of	O
a	O
class	O
type	O
that	O
implements	O
the	O
function-call	O
operator	O
,	O
allowing	O
the	O
object	O
to	O
be	O
used	O
within	O
expressions	O
using	O
the	O
same	O
syntax	O
as	O
a	O
function	O
call	O
.	O
</s>
<s>
Functors	B-Language
are	O
more	O
powerful	O
than	O
simple	O
function	B-Language
pointers	I-Language
,	O
being	O
able	O
to	O
contain	O
their	O
own	O
data	O
values	O
,	O
and	O
allowing	O
the	O
programmer	O
to	O
emulate	O
closures	B-Language
.	O
</s>
<s>
Many	O
"	O
pure	O
"	O
object-oriented	B-Language
languages	I-Language
do	O
not	O
support	O
function	B-Language
pointers	I-Language
.	O
</s>
<s>
Something	O
similar	O
can	O
be	O
implemented	O
in	O
these	O
kinds	O
of	O
languages	O
,	O
though	O
,	O
using	O
references	O
to	O
interfaces	B-Application
that	O
define	O
a	O
single	O
method	B-Language
(	O
member	O
function	O
)	O
.	O
</s>
<s>
CLI	B-Language
languages	I-Language
such	O
as	O
C#	B-Application
and	O
Visual	B-Language
Basic	I-Language
.NET	I-Language
implement	O
type-safe	B-Language
function	B-Language
pointers	I-Language
with	O
delegates	O
.	O
</s>
<s>
In	O
other	O
languages	O
that	O
support	O
first-class	B-Application
functions	I-Application
,	O
functions	O
are	O
regarded	O
as	O
data	O
,	O
and	O
can	O
be	O
passed	O
,	O
returned	O
,	O
and	O
created	O
dynamically	O
directly	O
by	O
other	O
functions	O
,	O
eliminating	O
the	O
need	O
for	O
function	B-Language
pointers	I-Language
.	O
</s>
<s>
Extensively	O
using	O
function	B-Language
pointers	I-Language
to	O
call	O
functions	O
may	O
produce	O
a	O
slow-down	O
for	O
the	O
code	O
on	O
modern	O
processors	O
,	O
because	O
a	O
branch	B-General_Concept
predictor	I-General_Concept
may	O
not	O
be	O
able	O
to	O
figure	O
out	O
where	O
to	O
branch	O
to	O
(	O
it	O
depends	O
on	O
the	O
value	O
of	O
the	O
function	B-Language
pointer	I-Language
at	O
run	O
time	O
)	O
although	O
this	B-Application
effect	O
can	O
be	O
overstated	O
as	O
it	O
is	O
often	O
amply	O
compensated	O
for	O
by	O
significantly	O
reduced	O
non-indexed	O
table	O
lookups	O
.	O
</s>
<s>
C++	B-Language
includes	O
support	O
for	O
object-oriented	B-Language
programming	I-Language
,	O
so	O
classes	O
can	O
have	O
methods	B-Language
(	O
usually	O
referred	O
to	O
as	O
member	O
functions	O
)	O
.	O
</s>
<s>
Non-static	O
member	O
functions	O
(	O
instance	B-Language
methods	I-Language
)	O
have	O
an	O
implicit	O
parameter	O
(	O
the	O
this	B-Application
pointer	I-Application
)	O
which	O
is	O
the	O
pointer	O
to	O
the	O
object	O
it	O
is	O
operating	O
on	O
,	O
so	O
the	O
type	O
of	O
the	O
object	O
must	O
be	O
included	O
as	O
part	O
of	O
the	O
type	O
of	O
the	O
function	B-Language
pointer	I-Language
.	O
</s>
<s>
The	O
method	B-Language
is	O
then	O
used	O
on	O
an	O
object	O
of	O
that	O
class	O
by	O
using	O
one	O
of	O
the	O
"	O
pointer-to-member	O
"	O
operators	O
:	O
.	O
</s>
<s>
Although	O
function	B-Language
pointers	I-Language
in	O
C	B-Language
and	O
C++	B-Language
can	O
be	O
implemented	O
as	O
simple	O
addresses	O
,	O
so	O
that	O
typically	O
sizeof(Fx )	O
==	O
sizeof( void	O
*	O
)	O
,	O
member	O
pointers	O
in	O
C++	B-Language
are	O
sometimes	O
implemented	O
as	O
"	O
fat	O
pointers	O
"	O
,	O
typically	O
two	O
or	O
three	O
times	O
the	O
size	O
of	O
a	O
simple	O
function	B-Language
pointer	I-Language
,	O
in	O
order	O
to	O
deal	O
with	O
virtual	B-Application
methods	I-Application
and	O
virtual	B-Application
inheritance	I-Application
.	O
</s>
<s>
In	O
C++	B-Language
,	O
in	O
addition	O
to	O
the	O
method	B-Language
used	O
in	O
C	B-Language
,	O
it	O
is	O
also	O
possible	O
to	O
use	O
the	O
C++	B-Language
standard	O
library	O
class	O
template	O
,	O
of	O
which	O
the	O
instances	O
are	O
function	B-Language
objects	I-Language
:	O
</s>
<s>
This	B-Application
is	O
how	O
C++	B-Language
uses	O
function	B-Language
pointers	I-Language
when	O
dealing	O
with	O
member	O
functions	O
of	O
classes	O
or	O
structs	B-Application
.	O
</s>
<s>
These	O
are	O
invoked	O
using	O
an	O
object	O
pointer	O
or	O
a	O
this	B-Application
call	O
.	O
</s>
<s>
They	O
are	O
type	B-Language
safe	I-Language
in	O
that	O
you	O
can	O
only	O
call	O
members	O
of	O
that	O
class	O
(	O
or	O
derivatives	O
)	O
using	O
a	O
pointer	O
of	O
that	O
type	O
.	O
</s>
<s>
This	B-Application
example	O
also	O
demonstrates	O
the	O
use	O
of	O
a	O
typedef	O
for	O
the	O
pointer	O
to	O
member	O
function	O
added	O
for	O
simplicity	O
.	O
</s>
<s>
Function	B-Language
pointers	I-Language
to	O
static	O
member	O
functions	O
are	O
done	O
in	O
the	O
traditional	O
'	O
C	B-Language
 '	O
style	O
because	O
there	O
is	O
no	O
object	O
pointer	O
for	O
this	B-Application
call	O
required	O
.	O
</s>
<s>
The	O
C	B-Language
and	O
C++	B-Language
syntax	O
given	O
above	O
is	O
the	O
canonical	O
one	O
used	O
in	O
all	O
the	O
textbooks	O
-	O
but	O
it	O
's	O
difficult	O
to	O
read	O
and	O
explain	O
.	O
</s>
<s>
Even	O
the	O
above	O
typedef	O
examples	O
use	O
this	B-Application
syntax	O
.	O
</s>
<s>
However	O
,	O
every	O
C	B-Language
and	O
C++	B-Language
compiler	O
supports	O
a	O
more	O
clear	O
and	O
concise	O
mechanism	O
to	O
declare	O
function	B-Language
pointers	I-Language
:	O
use	O
typedef	O
,	O
but	O
do	O
n't	O
store	O
the	O
pointer	O
as	O
part	O
of	O
the	O
definition	O
.	O
</s>
<s>
Note	O
that	O
the	O
only	O
way	O
this	B-Application
kind	O
of	O
typedef	O
can	O
actually	O
be	O
used	O
is	O
with	O
a	O
pointer	O
-	O
but	O
that	O
highlights	O
the	O
pointer-ness	O
of	O
it	O
.	O
</s>
