Recursion

Recursion is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem.

Python Example

# Define a recursive function that calculates a factorial, such as 5*4*3*2*1 = 120.
def factorial(number):
    
    # Set a value at which the function exits and prevents an infinite loop.
    if number == 1:
        return 1
    
    # If the current value is not at the exit point, continue.
    else:
        # Calculate a new value using this same function.
        new_value = number * factorial(number - 1)
  
        # Return the calculated value.
        return new_value

# Use the recursive function.
print(factorial(5))