Recursion/Recursive function in JavaScript ๐
Thread ๐งต
โ Recursion/Recursive function
โข Recursion is the concept that a function can be expressed in terms of itself.
โข A function that calls itself is called a recursive function.
โ Syntax for recursive function
function function_name() {
if(condition) //base case
{
//base case output
}else {
// stop calling function_name()
}
}
function_name(); //function call
โข Recursive function must have a base case when they return without calling the function again, otherwise they can never finish executing.
โข Example
function exp(num, pow){
if (pow == 1){
return num;
}else {
return num*exp(num, pow - 1);
}
}
exp(2,2);//4
โ Why to use recursion ?
โข Recursion adds clarity
โข Reduces the time needed to write and debug code
โข Reduces time complexity but not space complexity
โข Perform a unit of work multiple times.
End thread ๐งต
If this thread helpful for you then
1. Do Retweet the FIRST TWEET.
2. Follow me and turn on the notifications: @personalvipin
Thankyou โฃ๏ธ