<html lang="en"> <head> <title>Recursion - 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="Calling-Functions.html#Calling-Functions" title="Calling Functions"> <link rel="prev" href="Call-by-Value.html#Call-by-Value" title="Call by Value"> <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="Recursion"></a> <p> Previous: <a rel="previous" accesskey="p" href="Call-by-Value.html#Call-by-Value">Call by Value</a>, Up: <a rel="up" accesskey="u" href="Calling-Functions.html#Calling-Functions">Calling Functions</a> <hr> </div> <h4 class="subsection">8.2.2 Recursion</h4> <p><a name="index-factorial-function-454"></a> With some restrictions<a rel="footnote" href="#fn-1" name="fnd-1"><sup>1</sup></a>, recursive function calls are allowed. A <dfn>recursive function</dfn> is one which calls itself, either directly or indirectly. For example, here is an inefficient<a rel="footnote" href="#fn-2" name="fnd-2"><sup>2</sup></a> way to compute the factorial of a given integer: <pre class="example"> function retval = fact (n) if (n > 0) retval = n * fact (n-1); else retval = 1; endif endfunction </pre> <p>This function is recursive because it calls itself directly. It eventually terminates because each time it calls itself, it uses an argument that is one less than was used for the previous call. Once the argument is no longer greater than zero, it does not call itself, and the recursion ends. <p>The built-in variable <code>max_recursion_depth</code> specifies a limit to the recursion depth and prevents Octave from recursing infinitely. <!-- ov-usr-fcn.cc --> <p><a name="doc_002dmax_005frecursion_005fdepth"></a> <div class="defun"> — Built-in Function: <var>val</var> = <b>max_recursion_depth</b> ()<var><a name="index-max_005frecursion_005fdepth-455"></a></var><br> — Built-in Function: <var>old_val</var> = <b>max_recursion_depth</b> (<var>new_val</var>)<var><a name="index-max_005frecursion_005fdepth-456"></a></var><br> <blockquote><p>Query or set the internal limit on the number of times a function may be called recursively. If the limit is exceeded, an error message is printed and control returns to the top level. </p></blockquote></div> <div class="footnote"> <hr> <h4>Footnotes</h4><p class="footnote"><small>[<a name="fn-1" href="#fnd-1">1</a>]</small> Some of Octave's functions are implemented in terms of functions that cannot be called recursively. For example, the ODE solver <code>lsode</code> is ultimately implemented in a Fortran subroutine that cannot be called recursively, so <code>lsode</code> should not be called either directly or indirectly from within the user-supplied function that <code>lsode</code> requires. Doing so will result in an error.</p> <p class="footnote"><small>[<a name="fn-2" href="#fnd-2">2</a>]</small> It would be much better to use <code>prod (1:n)</code>, or <code>gamma (n+1)</code> instead, after first checking to ensure that the value <code>n</code> is actually a positive integer.</p> <hr></div> </body></html>