Recursion

Recursion (in programming) is when a method calls itself. Classic example - the factorial function.

n! = 1*2*3*...*(n-1)*n

Recursive definition: f(n) =

  • 1, if n=0
  • n*f(n-1), else

As a Java method:

public static int recursiveFactorial(int n){
   if (n==0) return 1;
   else return n*recursiveFactorial(n-1);
}

Content of a Recursive method

Base case(s)

Values of the input variables for which we perform no recursive calls are called base cases (there should be at least one base case). Every possible chain of recursive calls must eventually reach a base case.

Recursive calls

Calls to current method. Each recursive call should be defined so that it makes progress towards a base case.

Эта статья не закончена. Если хотите закончить ее, скачайте слайды лекции и продолжите переносить и форматировать информацию.

 
abstract_data_types_and_algorithms/recursion.txt · Последние изменения: 2009/09/10 05:47 От freetonik
 
За исключением случаев, когда указано иное, содержимое этой вики предоставляется на условиях следующей лицензии:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki