Skip to Content
bytegarden

Recursion

  • A recursive method is a method that calls itself

  • It continues calling itself until a base condition is met

  • Basic Structure:

    void fun(int n) {
        if (n > 0) {  // base condition
            System.out.println(n);  // task
            fun(n - 1);            // recursive call
        }
    }

Tradeoffs of Recursion

  1. Memory Usage
    • Recursion uses more memory (stack space)
    • Each recursive call adds to the stack
  2. Performance
    • Recursion may be slower due to function call overhead
    • Loops are generally more efficient