📄 m2_ftof.hlp
字号:
{smcl}
{* 18mar2005}{...}
{cmd:help m2 ftof}
{hline}
{* index pointers}{...}
{* index dereferencing}{...}
{title:Title}
{p 4 4 2}
{hi:[M-2] ftof -- Passing functions to functions}
{title:Syntax}
{it:example}{cmd:(}...{cmd:,} {cmd:&}{it:somefunction}{cmd:(),} ...{cmd:)}
where {it:example}{cmd:()} is coded
{cmd:function} {it:example}{cmd:(}...{cmd:,} {it:f}{cmd:,} ...{cmd:)}
{cmd:{c -(}}
...
{cmd:(*}{it:f}{cmd:)}{cmd:(}...{cmd:)}
...
{cmd:{c )-}}
{title:Description}
{p 4 4 2}
Functions can receive other functions as arguments.
{p 4 4 2}
Below is described (1) how to call a function that receives a function
as an argument and (2) how to write a function that receives a function
as an argument.
{title:Remarks}
{p 4 4 2}
Remarks are presented under the headings
{bf:Passing functions to functions}
{bf:Writing functions that receive functions}
{bf:Passing built-in functions}
{title:Passing functions to functions}
{p 4 4 2}
Someone has written a program that receives a function as an argument.
We will imagine that function is
{it:real scalar} {cmd:deriv(}{it:function}{cmd:()}{cmd:,} {it:x}{cmd:)}
{p 4 4 2}
and that {cmd:deriv()} numerically evaluates the derivative of
{it:function}{cmd:()} at {it:x}. The documentation for {cmd:deriv()} tells
you to write a function that takes a single argument and returns the
evaluation of the function at that argument, such as
{cmd}real scalar expratio(real scalar x)
{
return(exp(x)/exp(-x))
}{txt}
{p 4 4 2}
To call {cmd:deriv()} and have it evaluate the derivative of
{cmd:expratio()} at 3, you code
{cmd:deriv(&expratio(), 3)}
{p 4 4 2}
To pass a function to a function, you code {cmd:&} in front of the function's
name and {cmd:()} after.
Coding {cmd:&expratio()} passes the address of the function {cmd:expratio()} to
{cmd:deriv()}.
{title:Writing functions that receive functions, the simplified convention}
{p 4 4 2}
To receive a function, you include a variable among the program arguments
to receive the function -- we will use {it:f} -- and you then
code {cmd:(*}{it:f}{cmd:)(}...{cmd:)} to call the passed function. The code
for {cmd:deriv()} might read
{cmd}function deriv(f, x)
{
return( ((*f)(x+1e-6) - (*f)(x)) / 1e-6 )
}{txt}
{p 4 4 2}
or, if you prefer to be explicit about your declarations,
{cmd}real scalar deriv(pointer scalar f, real scalar x)
{
return( ((*f)(x+1e-6) - (*f)(x)) / 1e-6 )
}{txt}
{p 4 4 2}
or, if you prefer to be very explicit:
{cmd}real scalar deriv(pointer scalar(real scalar function) f,real scalar x)
{
return( ((*f)(x+1e-6) - (*f)(x)) / 1e-6 )
}{txt}
{p 4 4 2}
In any case, using pointers, you type {cmd:(*f)(}...{cmd:)} to execute the
function passed. See {bf:{help m2_pointers:[M-2] pointers}} for more
information.
{p 4 4 2}
{it:Aside:} The function {cmd:deriv()} would work but, because of the formula
it uses, would return very inaccurate results.
{title:Passing built-in functions}
{p 4 4 2}
You cannot pass built-in functions to other functions. For instance,
{bf:{help mf_exp:[M-5] exp()}} is built-in, which is revealed by
{bf:{help mata_which:[M-3] mata which}}:
: {cmd:mata which exp()}
exp(): built-in
{p 4 4 2}
Not all official functions are built-in. Many are implemented in Mata as
library functions, but {cmd:exp()} is built-in and coding {cmd:&exp()} will
result in an error. If you wanted to pass {cmd:exp()} to a function, create
your own version of it
: {cmd:function myexp(x) return(exp(x))}
{p 4 4 2}
and then pass {cmd:&myexp()}.
{title:Also see}
{p 4 13 2}
Manual: {hi:[M-2] ftof}
{p 4 13 2}
Online: help for
{bf:{help m2_intro:[M-2] intro}}
{p_end}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -