From Computer Science Wiki
Programming basics[1]

Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem (as opposed to iteration). The approach can be applied to many types of problems, and recursion is one of the central ideas of computer science [2]

A simple definition: Recursion is the process of a subroutine calling itself.


def factorial(n):
    if n == 1:
        return 1
        return n * factorial(n-1)

Identify a situation that requires the use of recursive thinking[edit]

Recursive thinking is a powerful tool that is useful in a variety of situations. Recursive thinking involves breaking a problem down into smaller and smaller subproblems until the subproblems are simple enough to solve directly. Here is an example of a situation that requires the use of recursive thinking:

Computing factorials: Factorial is a mathematical operation that is denoted by the symbol "!". For a non-negative integer n, n! is defined as the product of all positive integers less than or equal to n. For example, 5! = 5 × 4 × 3 × 2 × 1 = 120.

To compute the factorial of a number n using recursive thinking, we can use the following algorithm:

function factorial(n) {
  if (n === 0 || n === 1) {
    return 1;
  } else {
    return n * factorial(n - 1);

In this algorithm, we check if n is either 0 or 1. If it is, we return 1 since the factorial of 0 or 1 is 1. Otherwise, we compute n * factorial(n - 1) which calls the factorial function recursively with a smaller value of n. This process continues until n is reduced to 0 or 1, at which point the recursion stops and the final result is returned.

Note that the recursive algorithm for computing factorials is elegant and concise. However, it may not be the most efficient way to compute factorials for very large values of n. In those cases, an iterative algorithm may be more appropriate. Nonetheless, the recursive algorithm demonstrates the power of recursive thinking and how it can be used to solve problems in a concise and elegant way.


Another look at recursion[edit]


  • Identify a situation that requires the use of recursive thinking.
  • Identify recursive thinking in a specified problem solution.
  • Trace a recursive algorithm to express a solution to a problem.

See Also[edit]