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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Recursion (GNU Octave (version 10.3.0))</title>
<meta name="description" content="Recursion (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Recursion (GNU Octave (version 10.3.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Calling-Functions.html" rel="up" title="Calling Functions">
<link href="Access-via-Handle.html" rel="next" title="Access via Handle">
<link href="Call-by-Value.html" rel="prev" title="Call by Value">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<div class="subsection-level-extent" id="Recursion">
<div class="nav-panel">
<p>
Next: <a href="Access-via-Handle.html" accesskey="n" rel="next">Access via Handle</a>, Previous: <a href="Call-by-Value.html" accesskey="p" rel="prev">Call by Value</a>, Up: <a href="Calling-Functions.html" 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" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="Recursion-1"><span>8.2.2 Recursion<a class="copiable-link" href="#Recursion-1"> ¶</a></span></h4>
<a class="index-entry-id" id="index-factorial-function"></a>
<p>With some restrictions<a class="footnote" id="DOCF3" href="#FOOT3"><sup>3</sup></a>, recursive function calls are allowed. A
<em class="dfn">recursive function</em> is one which calls itself, either directly or
indirectly. For example, here is an inefficient<a class="footnote" id="DOCF4" href="#FOOT4"><sup>4</sup></a> way to compute the factorial of a given integer:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">function retval = fact (n)
if (n > 0)
retval = n * fact (n-1);
else
retval = 1;
endif
endfunction
</pre></div></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 function <code class="code">max_recursion_depth</code> may be used to specify a limit
to the recursion depth and prevents Octave from recursing infinitely.
Similarly, the function <code class="code">max_stack_depth</code> may be used to specify
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 class="anchor" id="XREFmax_005frecursion_005fdepth"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-max_005frecursion_005fdepth"><span><code class="def-type"><var class="var">val</var> =</code> <strong class="def-name">max_recursion_depth</strong> <code class="def-code-arguments">()</code><a class="copiable-link" href="#index-max_005frecursion_005fdepth"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-max_005frecursion_005fdepth-1"><span><code class="def-type"><var class="var">old_val</var> =</code> <strong class="def-name">max_recursion_depth</strong> <code class="def-code-arguments">(<var class="var">new_val</var>)</code><a class="copiable-link" href="#index-max_005frecursion_005fdepth-1"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-max_005frecursion_005fdepth-2"><span><code class="def-type"><var class="var">old_val</var> =</code> <strong class="def-name">max_recursion_depth</strong> <code class="def-code-arguments">(<var class="var">new_val</var>, "local")</code><a class="copiable-link" href="#index-max_005frecursion_005fdepth-2"> ¶</a></span></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 class="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 class="strong">See also:</strong> <a class="ref" href="#XREFmax_005fstack_005fdepth">max_stack_depth</a>.
</p></dd></dl>
<a class="anchor" id="XREFmax_005fstack_005fdepth"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-max_005fstack_005fdepth"><span><code class="def-type"><var class="var">val</var> =</code> <strong class="def-name">max_stack_depth</strong> <code class="def-code-arguments">()</code><a class="copiable-link" href="#index-max_005fstack_005fdepth"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-max_005fstack_005fdepth-1"><span><code class="def-type"><var class="var">old_val</var> =</code> <strong class="def-name">max_stack_depth</strong> <code class="def-code-arguments">(<var class="var">new_val</var>)</code><a class="copiable-link" href="#index-max_005fstack_005fdepth-1"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-max_005fstack_005fdepth-2"><span><code class="def-type"><var class="var">old_val</var> =</code> <strong class="def-name">max_stack_depth</strong> <code class="def-code-arguments">(<var class="var">new_val</var>, "local")</code><a class="copiable-link" href="#index-max_005fstack_005fdepth-2"> ¶</a></span></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 class="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 class="strong">See also:</strong> <a class="ref" href="#XREFmax_005frecursion_005fdepth">max_recursion_depth</a>.
</p></dd></dl>
</div>
<div class="footnotes-segment">
<hr>
<h4 class="footnotes-heading">Footnotes</h4>
<h5 class="footnote-body-heading"><a id="FOOT3" href="#DOCF3">(3)</a></h5>
<p>Some of Octave’s functions are
implemented in terms of functions that cannot be called recursively.
For example, the ODE solver <code class="code">lsode</code> is ultimately implemented in a
Fortran subroutine that cannot be called recursively, so <code class="code">lsode</code>
should not be called either directly or indirectly from within the
user-supplied function that <code class="code">lsode</code> requires. Doing so will result
in an error.</p>
<h5 class="footnote-body-heading"><a id="FOOT4" href="#DOCF4">(4)</a></h5>
<p>It would be
much better to use <code class="code">prod (1:n)</code>, or <code class="code">gamma (n+1)</code> instead,
after first checking to ensure that the value <code class="code">n</code> is actually a
positive integer.</p>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Access-via-Handle.html">Access via Handle</a>, Previous: <a href="Call-by-Value.html">Call by Value</a>, Up: <a href="Calling-Functions.html">Calling Functions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|