0% found this document useful (0 votes)
7 views1 page

Understanding Recursion with Factorial

Recursion is a programming technique where a function calls itself to break a problem into smaller parts, continuing until a base case is reached. An example is the factorial function, which multiplies a number by the factorial of the number minus one until it reaches one. The process is illustrated step-by-step, showing how factorial(5) results in 120.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

Understanding Recursion with Factorial

Recursion is a programming technique where a function calls itself to break a problem into smaller parts, continuing until a base case is reached. An example is the factorial function, which multiplies a number by the factorial of the number minus one until it reaches one. The process is illustrated step-by-step, showing how factorial(5) results in 120.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

■ Recursion (Definition)

A recursive function is a function that calls itself to solve a problem by breaking it into smaller,
simpler steps. It continues calling itself until it reaches a base case — the condition that tells it to
stop.

In short: Recursion = a function calling itself until a stopping condition is met.

■ Example: Factorial Using Recursion


function factorial(num) {
if (num === 1) return 1; // Base case
return num * factorial(num - 1); // Recursive call
}

[Link](factorial(5)); // Output: 120

■■ How it works (Step by Step)


factorial(5)
= 5 * factorial(4)
= 5 * 4 * factorial(3)
= 5 * 4 * 3 * factorial(2)
= 5 * 4 * 3 * 2 * factorial(1)
= 5 * 4 * 3 * 2 * 1
= 120 ■

You might also like