/// Factorial that shows each call and each return.
int factorialTraced(int n) {
    return factorialTraced(n, 0);
}

int factorialTraced(int n, int depth) {
    String indent = "  ".repeat(depth);
    IO.println(indent + "factorial(" + n + ") called");

    if (n <= 1) {
        IO.println(indent + "base case, returning 1");
        return 1;
    }

    int result = n * factorialTraced(n - 1, depth + 1);
    IO.println(indent + "returning " + n + " * factorial(" + (n - 1) + ") = "
            + result);
    return result;
}

void main() {
    factorialTraced(4);
}

// From Groundwork, chapter 12 (ch12-recursion-and-problem-solving), box 3.
// This example is from Groundwork, licensed CC BY-NC-SA 4.0.
// Run with: java ch12_03.java
