<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Recursion (GNU Octave (version 5.1.0))</title> <meta name="description" content="Recursion (GNU Octave (version 5.1.0))"> <meta name="keywords" content="Recursion (GNU Octave (version 5.1.0))"> <meta name="resource-type" content="document"> <meta name="distribution" content="global"> <meta name="Generator" content="makeinfo"> <link href="index.html#Top" rel="start" title="Top"> <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index"> <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> <link href="Calling-Functions.html#Calling-Functions" rel="up" title="Calling Functions"> <link href="Access-via-Handle.html#Access-via-Handle" rel="next" title="Access via Handle"> <link href="Call-by-Value.html#Call-by-Value" rel="prev" title="Call by Value"> <style type="text/css"> <!-- a.summary-letter {text-decoration: none} blockquote.indentedblock {margin-right: 0em} blockquote.smallindentedblock {margin-right: 0em; font-size: smaller} blockquote.smallquotation {font-size: smaller} div.display {margin-left: 3.2em} div.example {margin-left: 3.2em} div.lisp {margin-left: 3.2em} div.smalldisplay {margin-left: 3.2em} div.smallexample {margin-left: 3.2em} div.smalllisp {margin-left: 3.2em} kbd {font-style: oblique} pre.display {font-family: inherit} pre.format {font-family: inherit} pre.menu-comment {font-family: serif} pre.menu-preformatted {font-family: serif} pre.smalldisplay {font-family: inherit; font-size: smaller} pre.smallexample {font-size: smaller} pre.smallformat {font-family: inherit; font-size: smaller} pre.smalllisp {font-size: smaller} span.nolinebreak {white-space: nowrap} span.roman {font-family: initial; font-weight: normal} span.sansserif {font-family: sans-serif; font-weight: normal} ul.no-bullet {list-style: none} --> </style> <link rel="stylesheet" type="text/css" href="octave.css"> </head> <body lang="en"> <a name="Recursion"></a> <div class="header"> <p> Next: <a href="Access-via-Handle.html#Access-via-Handle" accesskey="n" rel="next">Access via Handle</a>, Previous: <a href="Call-by-Value.html#Call-by-Value" accesskey="p" rel="prev">Call by Value</a>, Up: <a href="Calling-Functions.html#Calling-Functions" accesskey="u" rel="up">Calling Functions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> <hr> <a name="Recursion-1"></a> <h4 class="subsection">8.2.2 Recursion</h4> <a name="index-factorial-function"></a> <p>With some restrictions<a name="DOCF3" href="#FOOT3"><sup>3</sup></a>, recursive function calls are allowed. A <em>recursive function</em> is one which calls itself, either directly or indirectly. For example, here is an inefficient<a name="DOCF4" href="#FOOT4"><sup>4</sup></a> way to compute the factorial of a given integer: </p> <div class="example"> <pre class="example">function retval = fact (n) if (n > 0) retval = n * fact (n-1); else retval = 1; endif endfunction </pre></div> <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> <p>The built-in variable <code>max_recursion_depth</code> specifies a limit to the recursion depth and prevents Octave from recursing infinitely. Similarly, the variable <code>max_stack_depth</code> specifies a limit to the depth of function calls, whether recursive or not. These limits help prevent stack overflow on the computer Octave is running on, so that instead of exiting with a signal, the interpreter will throw an error and return to the command prompt. </p> <a name="XREFmax_005frecursion_005fdepth"></a><dl> <dt><a name="index-max_005frecursion_005fdepth"></a><em><var>val</var> =</em> <strong>max_recursion_depth</strong> <em>()</em></dt> <dt><a name="index-max_005frecursion_005fdepth-1"></a><em><var>old_val</var> =</em> <strong>max_recursion_depth</strong> <em>(<var>new_val</var>)</em></dt> <dt><a name="index-max_005frecursion_005fdepth-2"></a><em></em> <strong>max_recursion_depth</strong> <em>(<var>new_val</var>, "local")</em></dt> <dd><p>Query or set the internal limit on the number of times a function may be called recursively. </p> <p>If the limit is exceeded, an error message is printed and control returns to the top level. </p> <p>When called from inside a function with the <code>"local"</code> option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function. </p> <p><strong>See also:</strong> <a href="#XREFmax_005fstack_005fdepth">max_stack_depth</a>. </p></dd></dl> <a name="XREFmax_005fstack_005fdepth"></a><dl> <dt><a name="index-max_005fstack_005fdepth"></a><em><var>val</var> =</em> <strong>max_stack_depth</strong> <em>()</em></dt> <dt><a name="index-max_005fstack_005fdepth-1"></a><em><var>old_val</var> =</em> <strong>max_stack_depth</strong> <em>(<var>new_val</var>)</em></dt> <dt><a name="index-max_005fstack_005fdepth-2"></a><em></em> <strong>max_stack_depth</strong> <em>(<var>new_val</var>, "local")</em></dt> <dd><p>Query or set the internal limit on the number of times a function may be called recursively. </p> <p>If the limit is exceeded, an error message is printed and control returns to the top level. </p> <p>When called from inside a function with the <code>"local"</code> option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function. </p> <p><strong>See also:</strong> <a href="#XREFmax_005frecursion_005fdepth">max_recursion_depth</a>. </p></dd></dl> <div class="footnote"> <hr> <h4 class="footnotes-heading">Footnotes</h4> <h3><a name="FOOT3" href="#DOCF3">(3)</a></h3> <p>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> <h3><a name="FOOT4" href="#DOCF4">(4)</a></h3> <p>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> </div> <hr> <div class="header"> <p> Next: <a href="Access-via-Handle.html#Access-via-Handle" accesskey="n" rel="next">Access via Handle</a>, Previous: <a href="Call-by-Value.html#Call-by-Value" accesskey="p" rel="prev">Call by Value</a>, Up: <a href="Calling-Functions.html#Calling-Functions" accesskey="u" rel="up">Calling Functions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> </body> </html>