Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > b38d2da330d1936e5ab1307c039c4941 > files > 434

octave-doc-3.6.4-3.mga4.noarch.rpm

<html lang="en">
<head>
<title>Recursion - GNU Octave</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="GNU Octave">
<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:&nbsp;<a rel="previous" accesskey="p" href="Call-by-Value.html#Call-by-Value">Call by Value</a>,
Up:&nbsp;<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-527"></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 &gt; 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.

<!-- max_recursion_depth src/pt-eval.cc -->
   <p><a name="doc_002dmax_005frecursion_005fdepth"></a>

<div class="defun">
&mdash; Built-in Function: <var>val</var> = <b>max_recursion_depth</b> ()<var><a name="index-max_005frecursion_005fdepth-528"></a></var><br>
&mdash; Built-in Function: <var>old_val</var> = <b>max_recursion_depth</b> (<var>new_val</var>)<var><a name="index-max_005frecursion_005fdepth-529"></a></var><br>
&mdash; Built-in Function:  <b>max_recursion_depth</b> (<var>new_val, "local"</var>)<var><a name="index-max_005frecursion_005fdepth-530"></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>When called from inside a function with the "local" 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></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>