 Hello everyone, I am Mrs. Meenakshi Sri Gandhi from Walsham Institute of Technology, Shalapur. Welcome to the video lecture on stack application, stack in recursion. Learning outcome, at the end of this session, student will be able to explain the application of stack in recursion. Let's see the application of stack. As we already know that stack is a repo structure, that is the last item to be inserted is the first item to be removed. Its application can be seen in execution of sub-programs in a computer system, where before a sub-program executes a task, it pushes its written address on the stack and after the execution of a task, it comes back to the stack and pops a written address and then it goes to the main function. Application of stack can also be seen in recursion. Expression is nothing but when a function calls itself. Here also before the function executed task, it saves a written address on the stack and after the execution of a task, it comes to the stack and pops a written address. Stack application can also be seen in expression evaluation of post-fix string and expression conversion from infix notation to post-fix notation. Let's see what is recursion is. Recursion is nothing but something which is expressed in terms of itself. So it is useful in solving problems that can be broken down into smaller problems of same kind. So recursion is a process of expressing a function in terms of itself. Here you can see that a function f of x is repeatedly calling itself, which is representing recursion. Let's see the difference between iterative and recursive definition. To explain this difference, we are taking the factorial example. So let's see the factorial using iterative definition. Here n factorial is equal to 1 if n is equal to 0 and if n is greater than 0 then n is multiplied to n minus 1, which is again multiplied to n minus 2. This process is continued till 1 is reached. For example, to calculate 4 factorial, 4 is multiplied to 4 minus 1 that is 3. Again 3 is multiplied to 3 minus 1 that is 2 and 2 is multiplied to 2 minus 1 that is equal to 1, which results to 24. To find the factorial of 3, 3 is multiplied to 3 minus 1 that is 2. 2 is again multiplied to 2 minus 1 that is 1, which gives the result as 6. To find the 2 factorial, 2 is multiplied to 2 minus 1 that is 1, which results to 2. So in short, we can see that iterative process gives the explicit repetition of a process until a certain condition is met. So in this example, here the repetition is done till 1 is reached. Let's see the factorial using recursive definition. So n factorial is equal to 1 if n is equal to 0 and if n is greater than 0 then n is multiplied to n minus 1 factorial. Let's see the example by using recursive definition. So 4 factorial is calculated as 4 into 3 factorial and 3 factorial is equal to 3 into 2 factorial and 2 factorial is equal to 2 into 1 factorial and 1 factorial is equal to 1 into 0 factorial. So a recursive definition defines an object in terms of a simple case of itself. Try to think and answer to write a C routine to find the factorial of a number using recursive definition. Here we are trying to write the recursive routine for finding factorial of a number. So we are finding the factorial of n for this a variable x has been declared. If the number is equal to 0 then written a 1. If number is not equal to 0 if it is greater than 0 then n minus 1 value is stored in x. Then we are returning n into factorial of x. Here you can see that the factorial function is invoked again but with x which is nothing but n minus 1. For writing any recursive routine there are two conditions that must be satisfied for any recursive procedure. The first is the base case. The base case is the smallest value which is executed without recursion and which is the decision criterion for stopping the recursive procedure. And the second is the general method which makes any particular case to reach nearer in some sense to the base case. And in this factorial function each time the function call itself its argument is decremented by 1 so that the argument becomes smaller and ultimately reach to the value 0 which is the base case in this case. Let's see the general algorithm for recursive procedure. Before any recursive procedure is executed first we need to save the parameters, local variables and written address on the stack. Then we need to test for the base criterion. If the base criterion has been reached then we need to perform the final computation and we need to go to step 3. If the base criterion has not been reached in that case we need to perform the partial computation and go to step 1 and repeat the process again with reduced parameter values. If the base criterion has been reached in that case we need to restore the most recently saved parameters, local variables and written address then after that we need to go to this written address. Now let's see how stack is used in recursion. To explain the same again we are taking the factorial example. Here we are trying to show you how the stack grows for each recursion call. So initially you can see that these are the stack elements. Ivan is representing the initial written address. N is representing the number 4 for which the factorial has to be calculated and x is equal to 3 which is equal to n-1. So in this n is 4, so 4-1 is equal to 3. Now top is pointing to factorial which has to be calculated. For the second recursion call you can see that the stack has been grown by size 4 again. So ito is indicating the written address and n is now reduced to 3 and x is now equal to n-1 that is equal to 2. Now top is pointing to factorial which has to be calculated. After the fourth recursion call you can see that the size of the stack has still grown. So ito is indicating the written address and n is equal to 0 which is the base case and top is now pointing to factorial which has to be calculated. So this is how the stack grows for each recursion call. Now in this we will see how the stack shrinks after each recursion call is completed. So initially when n is equal to 0 which is indicating the base case in that case our written value is equal to 1. After the next recursion call has been completed you can see that the factorial 1 and the number n are multiplied and the written value is 1. After the next recursion call has been completed the factorial 1 and the number n is equal to 2 these are multiplied and the written value is 2. After the next recursion call has been completed factorial 2 and the number n equal to 3 these are multiplied and the written value is equal to 6. And after the last regression call has been completed the factorial 6 and the number n equal to 4 these are multiplied and the written value is 24. So, you can see that Ivan is equal to 24 which is the final value which is returned to the main function which is the result for the factorial of 4. Let's see the flowchart for a recursive process. Before executing any recursive process first we need to save the formal parameters, local variables and written address on the stack. Then we need to test for the base case. If the base case has not been reached then in that case we need to perform the partial computation. Then we need to call the recursive procedure again with reduced parameter values. But if the base case has been reached then we need to perform the final computation and then we need to restore the most recently saved formal parameters, local variables and written address. And after this we need to go to this written address. These are my references. Thank you.