Junior — Middle
Can you explain what a recursive call is in programming?
sobes.tech AI
Answer from AI
Recursive call — this is when a function calls itself to solve a problem. Usually, recursion is used to break down a complex problem into simpler subproblems of the same type.
Example: calculating the factorial of a number n (n!) — the product of all numbers from 1 to n.
function factorial(n) {
if (n <= 1) return 1; // base case
return n * factorial(n - 1); // recursive call
}
console.log(factorial(5)); // 120
It is important that the recursive function has a base case, at which it stops calling itself, otherwise, there will be infinite recursion and stack overflow.