|
| 1 | +--- |
| 2 | +Title: 'Recursion' |
| 3 | +Description: 'Recursion is a technique where a function calls itself to solve smaller instances of the same problem.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Web Development' |
| 7 | +Tags: |
| 8 | + - 'Functions' |
| 9 | + - 'Loops' |
| 10 | + - 'Recursion' |
| 11 | +CatalogContent: |
| 12 | + - 'introduction-to-javascript' |
| 13 | + - 'paths/front-end-engineer-career-path' |
| 14 | +--- |
| 15 | + |
| 16 | +**Recursion** is a technique where a [function](https://www.codecademy.com/resources/docs/javascript/functions) calls itself to solve smaller instances of the same problem until a specific condition called the base case is met. It’s commonly used when a task can be broken down into repeated sub-tasks. |
| 17 | + |
| 18 | +A recursive function must always have two parts: |
| 19 | + |
| 20 | +- **Base case:** Defines when the recursion should stop to prevent infinite calls. |
| 21 | +- **Recursive call:** The function calls itself to continue solving the smaller problem until the base case is reached. |
| 22 | + |
| 23 | +If the base case is missing, the function will keep calling itself indefinitely, causing a stack overflow error. |
| 24 | + |
| 25 | +## Syntax |
| 26 | + |
| 27 | +```pseudo |
| 28 | +function recursiveFunction() { |
| 29 | + // Code block to execute |
| 30 | + recursiveFunction(); // Recursive call |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +## Example |
| 35 | + |
| 36 | +In this example, the function prints numbers from 1 to 10 by calling itself until a condition is met: |
| 37 | + |
| 38 | +```js |
| 39 | +let count = 0; |
| 40 | + |
| 41 | +function countToTen() { |
| 42 | + if (count < 10) { |
| 43 | + count = count + 1; |
| 44 | + console.log(count); |
| 45 | + countToTen(); // Recursive call |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +countToTen(); |
| 50 | +``` |
| 51 | + |
| 52 | +The output would be: |
| 53 | + |
| 54 | +```shell |
| 55 | +1 |
| 56 | +2 |
| 57 | +3 |
| 58 | +4 |
| 59 | +5 |
| 60 | +6 |
| 61 | +7 |
| 62 | +8 |
| 63 | +9 |
| 64 | +10 |
| 65 | +``` |
| 66 | + |
| 67 | +Here’s what happens step by step: |
| 68 | + |
| 69 | +- A variable `count` is initialized outside the function. |
| 70 | +- The function checks if `count` is less than 10. |
| 71 | +- If true, it increments `count`, prints it, and calls itself again. |
| 72 | +- When `count` reaches 10, the base case condition fails, and recursion stops. |
| 73 | + |
| 74 | +This process is similar to how loops work, but recursion uses function calls instead of loop structures. |
| 75 | + |
| 76 | +## Codebyte Example |
| 77 | + |
| 78 | +In this example, the function repeatedly calls itself to print numbers from 1 to 10, stopping once the base case condition (`count < 10`) is no longer true: |
| 79 | + |
| 80 | +```codebyte/javascript |
| 81 | +let count = 0; |
| 82 | +
|
| 83 | +function countToTen() { |
| 84 | + if (count < 10) { |
| 85 | + count++; |
| 86 | + console.log(count); |
| 87 | + countToTen(); |
| 88 | + } |
| 89 | +} |
| 90 | +
|
| 91 | +countToTen(); |
| 92 | +``` |
0 commit comments