-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path71_recursive_function.js
27 lines (19 loc) · 1.9 KB
/
71_recursive_function.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Recursive functions are functions that call themselves.This can be useful when you need to solve a problem that can be broken down into smaller sub - problems, each of which can be solved in the same way as the original problem.Here's an example:
// function factorial(n) {
// if (n === 0) {
// return 1;
// } else {
// return n * factorial(n - 1);
// }
// }
// This is a recursive function that calculates the factorial of a number.The factorial of a number n is the product of all the integers from 1 to n.For example, the factorial of 5 is 5 x 4 x 3 x 2 x 1, which is 120.
// Let's break down how this function works:
// The function factorial takes one argument, n.
// If n is 0, the function returns 1. This is the base case of the recursive function.
// If n is not 0, the function returns n times the result of calling factorial with n - 1. This is the recursive case.
// When factorial is called with a number, it first checks if the number is 0. If it is, the function returns 1. If it's not, the function returns n times the result of calling factorial with n - 1. This process continues until n is 0, at which point the function starts returning the products of the previous calls to factorial. This is because each call to factorial is waiting for the next call to return before it can finish.
// Here's an example of how to call the factorial function:
// let result = factorial(5);
// console.log(result); // Output: 120
// This will calculate the factorial of 5 using the recursive function we defined earlier.
// One important thing to note is that recursive functions can be very memory - intensive if the recursion goes too deep.This is because each function call is added to the call stack, and if the call stack gets too large, it can cause a stack overflow error.It's important to keep this in mind when using recursive functions, and to consider using iterative solutions when possible.