1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
<html lang="en">
<head>
<title>Function Handles - GNU Octave</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="GNU Octave">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Function-Handles-Inline-Functions-and-Anonymous-Functions.html#Function-Handles-Inline-Functions-and-Anonymous-Functions" title="Function Handles Inline Functions and Anonymous Functions">
<link rel="next" href="Anonymous-Functions.html#Anonymous-Functions" title="Anonymous Functions">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<a name="Function-Handles"></a>
<p>
Next: <a rel="next" accesskey="n" href="Anonymous-Functions.html#Anonymous-Functions">Anonymous Functions</a>,
Up: <a rel="up" accesskey="u" href="Function-Handles-Inline-Functions-and-Anonymous-Functions.html#Function-Handles-Inline-Functions-and-Anonymous-Functions">Function Handles Inline Functions and Anonymous Functions</a>
<hr>
</div>
<h4 class="subsection">11.10.1 Function Handles</h4>
<p>A function handle is a pointer to another function and is defined with
the syntax
<pre class="example"> @<var>function-name</var>
</pre>
<p class="noindent">For example,
<pre class="example"> f = @sin;
</pre>
<p class="noindent">creates a function handle called <code>f</code> that refers to the
function <code>sin</code>.
<p>Function handles are used to call other functions indirectly, or to pass
a function as an argument to another function like <code>quad</code> or
<code>fsolve</code>. For example:
<pre class="example"> f = @sin;
quad (f, 0, pi)
⇒ 2
</pre>
<p>You may use <code>feval</code> to call a function using function handle, or
simply write the name of the function handle followed by an argument
list. If there are no arguments, you must use an empty argument list
‘<samp><span class="samp">()</span></samp>’. For example:
<pre class="example"> f = @sin;
feval (f, pi/4)
⇒ 0.70711
f (pi/4)
⇒ 0.70711
</pre>
<!-- is_function_handle src/ov-fcn-handle.cc -->
<p><a name="doc_002dis_005ffunction_005fhandle"></a>
<div class="defun">
— Built-in Function: <b>is_function_handle</b> (<var>x</var>)<var><a name="index-is_005ffunction_005fhandle-790"></a></var><br>
<blockquote><p>Return true if <var>x</var> is a function handle.
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->
<p class="noindent"><strong>See also:</strong> <a href="doc_002disa.html#doc_002disa">isa</a>, <a href="doc_002dtypeinfo.html#doc_002dtypeinfo">typeinfo</a>, <a href="doc_002dclass.html#doc_002dclass">class</a>.
</p></blockquote></div>
<!-- functions src/ov-fcn-handle.cc -->
<p><a name="doc_002dfunctions"></a>
<div class="defun">
— Built-in Function: <b>functions</b> (<var>fcn_handle</var>)<var><a name="index-functions-791"></a></var><br>
<blockquote><p>Return a struct containing information about the function handle
<var>fcn_handle</var>.
</p></blockquote></div>
<!-- func2str src/ov-fcn-handle.cc -->
<p><a name="doc_002dfunc2str"></a>
<div class="defun">
— Built-in Function: <b>func2str</b> (<var>fcn_handle</var>)<var><a name="index-func2str-792"></a></var><br>
<blockquote><p>Return a string containing the name of the function referenced by
the function handle <var>fcn_handle</var>.
</p></blockquote></div>
<!-- str2func src/ov-fcn-handle.cc -->
<p><a name="doc_002dstr2func"></a>
<div class="defun">
— Built-in Function: <b>str2func</b> (<var>fcn_name</var>)<var><a name="index-str2func-793"></a></var><br>
— Built-in Function: <b>str2func</b> (<var>fcn_name, "global"</var>)<var><a name="index-str2func-794"></a></var><br>
<blockquote><p>Return a function handle constructed from the string <var>fcn_name</var>.
If the optional "global" argument is passed, locally visible functions
are ignored in the lookup.
</p></blockquote></div>
</body></html>
|