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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Recursion (GNU Octave (version 6.2.0))</title>
<meta name="description" content="Recursion (GNU Octave (version 6.2.0))">
<meta name="keywords" content="Recursion (GNU Octave (version 6.2.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<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.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {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}
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">
<span id="Recursion"></span><div class="header">
<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>
<span id="Recursion-1"></span><h4 class="subsection">8.2.2 Recursion</h4>
<span id="index-factorial-function"></span>
<p>With some restrictions<a id="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 id="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 function <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>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>
<span id="XREFmax_005frecursion_005fdepth"></span><dl>
<dt id="index-max_005frecursion_005fdepth">: <em><var>val</var> =</em> <strong>max_recursion_depth</strong> <em>()</em></dt>
<dt id="index-max_005frecursion_005fdepth-1">: <em><var>old_val</var> =</em> <strong>max_recursion_depth</strong> <em>(<var>new_val</var>)</em></dt>
<dt id="index-max_005frecursion_005fdepth-2">: <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>
<span id="XREFmax_005fstack_005fdepth"></span><dl>
<dt id="index-max_005fstack_005fdepth">: <em><var>val</var> =</em> <strong>max_stack_depth</strong> <em>()</em></dt>
<dt id="index-max_005fstack_005fdepth-1">: <em><var>old_val</var> =</em> <strong>max_stack_depth</strong> <em>(<var>new_val</var>)</em></dt>
<dt id="index-max_005fstack_005fdepth-2">: <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>
<h5><a id="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>
<h5><a id="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" 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>
</body>
</html>
|