Introduction
In data structure and algorithms, the concept of Recursion plays an important role. In this article, we will go over a brief introduction to Recursion.
Recursion is a technique involving the use of a procedure, subroutine, function, or algorithm that calls itself in a step along with some termination condition.
Properties
 The same operation is performed multiple times with different input
 In every step, we try to make the problem smaller
 We need to have a base condition that tells the system to stop the recursion
Let's take a small example:
(Here I will write the algorithm and later will implement it with Javascript, but you can choose your own language).
We will use the example of the summation of 10 numbers in an array with Recursion
Algorithm
 DisplayArray(arr, index) :
 start
 If(index< 0) :
 return false
 DisplayArray(arr, index1)
 Print(arr[index])
 End
Let’s check whether or not this Algorithm satisfies the Recursion properties:
 The DisplayArray method is called with different parameters. In each call, the index is reduced, hence satisfying the first property
 We are reducing the index every time, hence the second property is also satisfied
 If the index becomes less than 0 then the algorithm will break, hence we can say this is the Base condition. Therefore, the third property is also satisfied.
Format of a Recursion Function
 Recursive Case: the case where the algorithm recurs (i.e method will call itself)
 Base Case: the case where the method will not call itself, (i.e terminating condition)
Therefore...
SampleRecursion(parameter1, parameter2 ,…):
If(base case is satisfied):
Return some value
Else :
SampleRecursion(modified parameter)
Recursion Working Principle
In a programming language, a method is stored in a stack that follows Last In First Out (LIFO). Whenever a method is called but, due to some condition, it must be executed down the road, the method is saved in stack memory. A program counter (PC) will hold the memory location so that it will be referenced later on.
Let's discuss the subject with an example:
 Tst(n):
 start
 If(n < 1):
 Return
 Else:
 Tst(n1)
 Print(“Hi” + n)
 End

 Main():
 start
 Tst(3)
 end


Now let's see what’s happening
 Main calls Tst with the parameter 3 and the controls go to method Tst, hence the Main method is not finished yet, so it gets saved into the stack memory
 Since at first, Tst(3) is called (n<1) is not satisfied so it will go to the Else. Here, it will call itself again with n1. Hence, the Tst(3) is function is not completed since it does not go to the next line, Print(“Hi”+n). Tst(3) will be stored into the stack memory to be executed later.
 Next, in a similar fashion, Tst(2) and Tst(1) are called and get stored in the stack
 When Tst(0) is called, (n<1) which is the base condition that meets the criteria. Hence, it’s going to terminate the algorithm. Here the program counter takes control and finds in the stack when the function was called at last time and with what parameter (if there is any).
 Since as discussed, methods get stored in stack memory in a LIFO pattern, Tst(1) will be executed first.
 Tst(1) is executed, which means the remaining part of the method to print, “Hi 1”, will be printed. Now that Tst(1) is completed, it will be popped out of memory and the PC will execute Tst(2), and then Tst(3).
Here is the code snippet of the following:
 function Tst(n){
 if(n<1)
 return
 else{
 Tst(n1)
 }
 console.log("Hi"+n)
 }

 Tst(3)