8.2.2 Recursion

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

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

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 function max_recursion_depth may be used to specify a limit to the recursion depth and prevents Octave from recursing infinitely. Similarly, the function max_stack_depth 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.

: val = max_recursion_depth ()
: old_val = max_recursion_depth (new_val)
: max_recursion_depth (new_val, "local")

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.

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.

See also: max_stack_depth.

: val = max_stack_depth ()
: old_val = max_stack_depth (new_val)
: max_stack_depth (new_val, "local")

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.

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.

See also: max_recursion_depth.

Footnotes

(3)

Some of Octave’s functions are implemented in terms of functions that cannot be called recursively. For example, the ODE solver lsode is ultimately implemented in a Fortran subroutine that cannot be called recursively, so lsode should not be called either directly or indirectly from within the user-supplied function that lsode requires. Doing so will result in an error.

(4)

It would be much better to use prod (1:n), or gamma (n+1) instead, after first checking to ensure that the value n is actually a positive integer.

© 1996–2020 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/v6.3.0/Recursion.html