Search⌘ K

Direct vs. Indirect Recursion

Explore the concepts of direct and indirect recursion in this lesson. Understand how a function can call itself directly or through another function, with practical JavaScript examples that print natural numbers. Gain clarity on recursion patterns to prepare for coding interviews.

Direct Recursion

Direct recursion occurs when a function calls itself.

This results in a one-step recursive call: the function makes a recursive call inside its own function body.

Syntax of Direct Recursion

function function1(p1, p2, ..., pn) {
  // Some code here
  function1(p1, p2, ..., pn);
  // Some code here
}

Printing Natural Numbers from 11 to nn Using Direct Recursion

Let’s have a look at an example that prints natural numbers from ...