<html lang="en"> <head> <title>Calling a Function by its Name - Untitled</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Untitled"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Evaluation.html#Evaluation" title="Evaluation"> <link rel="next" href="Evaluation-in-a-Different-Context.html#Evaluation-in-a-Different-Context" title="Evaluation in a Different Context"> <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="Calling-a-Function-by-its-Name"></a> <p> Next: <a rel="next" accesskey="n" href="Evaluation-in-a-Different-Context.html#Evaluation-in-a-Different-Context">Evaluation in a Different Context</a>, Up: <a rel="up" accesskey="u" href="Evaluation.html#Evaluation">Evaluation</a> <hr> </div> <h3 class="section">9.1 Calling a Function by its Name</h3> <p>The <code>feval</code> function allows you to call a function from a string containing its name. This is useful when writing a function that needs to call user-supplied functions. The <code>feval</code> function takes the name of the function to call as its first argument, and the remaining arguments are given to the function. <p>The following example is a simple-minded function using <code>feval</code> that finds the root of a user-supplied function of one variable using Newton's method. <pre class="example"> <a name="index-Fordyce_002c-A_002e-P_002e-547"></a><a name="index-newtroot-548"></a>function result = newtroot (fname, x) # usage: newtroot (fname, x) # # fname : a string naming a function f(x). # x : initial guess delta = tol = sqrt (eps); maxit = 200; fx = feval (fname, x); for i = 1:maxit if (abs (fx) < tol) result = x; return; else fx_new = feval (fname, x + delta); deriv = (fx_new - fx) / delta; x = x - fx / deriv; fx = fx_new; endif endfor result = x; endfunction </pre> <p>Note that this is only meant to be an example of calling user-supplied functions and should not be taken too seriously. In addition to using a more robust algorithm, any serious code would check the number and type of all the arguments, ensure that the supplied function really was a function, etc. See <a href="Predicates-for-Numeric-Objects.html#Predicates-for-Numeric-Objects">Predicates for Numeric Objects</a>, for example, for a list of predicates for numeric objects, and see <a href="Status-of-Variables.html#Status-of-Variables">Status of Variables</a>, for a description of the <code>exist</code> function. <!-- parse.cc --> <p><a name="doc_002dfeval"></a> <div class="defun"> — Built-in Function: <b>feval</b> (<var>name, <small class="dots">...</small></var>)<var><a name="index-feval-549"></a></var><br> <blockquote><p>Evaluate the function named <var>name</var>. Any arguments after the first are passed on to the named function. For example, <pre class="example"> feval ("acos", -1) ⇒ 3.1416 </pre> <p class="noindent">calls the function <code>acos</code> with the argument ‘<samp><span class="samp">-1</span></samp>’. <p>The function <code>feval</code> is necessary in order to be able to write functions that call user-supplied functions, because Octave does not have a way to declare a pointer to a function (like C) or to declare a special kind of variable that can be used to hold the name of a function (like <code>EXTERNAL</code> in Fortran). Instead, you must refer to functions by name, and use <code>feval</code> to call them. </p></blockquote></div> <p>A similar function <code>run</code> exists for calling user script files, that are not necessarily on the user path <!-- ./miscellaneous/run.m --> <p><a name="doc_002drun"></a> <div class="defun"> — Function File: <b>run</b> (<var>f</var>)<var><a name="index-run-550"></a></var><br> — Command: <b>run</b><var> f<a name="index-run-551"></a></var><br> <blockquote><p>Run scripts in the current workspace that are not necessarily on the path. If <var>f</var> is the script to run, including its path, then <code>run</code> change the directory to the directory where <var>f</var> is found. <code>run</code> then executes the script, and returns to the original directory. <!-- 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_002dsystem.html#doc_002dsystem">system</a>. </p></blockquote></div> </body></html>