Recursion in plain English

suggest change

Recursion can be defined as:

A method that calls itself until a specific condition is met.

An excellent and simple example of recursion is a method that will get the factorial of a given number:

public int Factorial(int number)
{
    return number == 0 ? 1 : n * Factorial(number - 1);
}

In this method, we can see that the method will take an argument, number.

Step by step:

Given the example, executing Factorial(4)

  1. Is number (4) == 1?
  2. No? return 4 * Factorial(number-1) (3)
  3. Because the method is called once again, it now repeats the first step using Factorial(3) as the new argument.
  4. This continues until Factorial(1) is executed and number (1) == 1 returns 1.
  5. Overall, the calculation “builds up” 4 * 3 * 2 * 1 and finally returns 24.

The key to understanding recursion is that the method calls a new instance of itself. After returning, the execution of the calling instance continues.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Recursion:
* Recursion in plain English

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
52 Recursion
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
147 C# Script