Recursion

Recursion #

Definition. A function/method is recursive if it calls itself, directly or indirectly.

Example. The following method (in pseudocode) is recursive:

    myMethod() {
        ...
        myMethod()
        ...
    }

Example. The following two methods (in pseudocode) are recursive:

    myMethod1() {
        ...
        myMethod2()
        ...
    }

    myMethod2() {
        ...
        myMethod1()
        ...
    }

Usage. In theory, a recursive algorithm can be rewritten using loops only (and conversely).

In practice, some problems are easier to solve recursively. This includes many problem that involve tree-like structures (e.g. a folder, a JSON or XML document, an algebraic expression, etc.), and more generally graphs.