- publishing free software manuals
GNU Octave Manual Version 3
by John W. Eaton, David Bateman, Søren Hauberg
Paperback (6"x9"), 568 pages
ISBN 095461206X
RRP £24.95 ($39.95)

Get a printed copy>>>

8.2.2 Recursion

With some restrictions(3), recursive function calls are allowed. A recursive function is one which calls itself, either directly or indirectly. For example, here is an inefficient(4) way to compute the factorial of a given integer:

function retval = fact (n)
  if (n > 0)
    retval = n * fact (n-1);
    retval = 1;

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.

The built-in variable max_recursion_depth specifies a limit to the recursion depth and prevents Octave from recursing infinitely.

Built-in Function: val = max_recursion_depth ()
Built-in Function: old_val = max_recursion_depth (new_val)
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.

ISBN 095461206XGNU Octave Manual Version 3See the print edition