 Welcome to the session Recursion Introduction. At the end of this session, the student will be able to implement C programs using recursion method. First we will see what is the recursion method. In recursion, the function only knows how to solve the simplest and smallest case of the problem. Recursion is function who is calling itself. A recursion function will call when it has to solve a complex problems. Now in this example, we will see that task is to pick the all fruits from the tree and get down. It will take 3 to 4 steps, ok. So it will be cut down into the smallest part that he will pick the fruits and he will step down one step at a time. Then he will count, ok, still 2 more are there. Again he will go one step down like this. He will continue to get down from the ladder one step at a time. So hit this. So this is the, this is that recursion you are calling that at small step you are taking every time to complete your big task. I will take one more example here. Now when I want to pronounce the item, then it is a bigger one. So I will try that from H-A-M then B-U-R then G-E-R then I can get that hamburger. So what I have done, I have cut down this word into the parts. First I have cut down into the 3 parts. Then I have cut down again. One more recursion I am calling that is H-A-M then it will be ham, then burr it will be going for B-U-R then G-E-R. Then we can recollect it ham, burr, gur. So the recursive answer will be for this is ham then recursive answer will be burr and here it is a gur. So again we will collect this all and then we will call ham, burr, gur. So this is a small example of the recursion. Now we will take one example in the C program that simple we are printing a string using a tell recursion. What is the tell recursion? Your recursion step is at the end then it is called as a tell recursion. It is, now this program is for we are having a string India and we need to print it. We are calling this function. As I said in the hamburger we are going to go step by step like this here. What will happen? Your program will cut down India into single character first I then N then D then I then A. We are printing a pointer where our pointer is pointing and then we are incrementing that by pointer by one and important thing in the recursion part that we are using stack. Now what is the stack? Stack is one place in your memory. In your memory what is there? We are having we are writing first program that is also in the memory then whatever this variables are there we are declared that is also stored into the memory. So that place is another place where we are storing this static variables. One place is where we are writing our program. One place is where in the memory where we are keeping our all static variables. And then we are having the stack. These are the parts of your memory where we are maintaining all this thing. In the function when the it is locating to the India I will be there first it will ask to print it where your pointer is there. So in the stack what will ever it will be active when I am executing this program and in that first case is that your pointer is showing that I is there first character in this. So immediately it will telling that print it. So our output will be printing I then again next step is that we are calling string print s plus 1 means we are incrementing the pointer and again we are calling string print the same function. So what will happen? It will go to the next character n then again we are going for the printing. So it will be printing n. Next again it will call then it will be going for the d and again immediately we are telling that go and print it so d will be there then again it will call the same function then it will be going for I and again we are saying that print it so it will be printing and then again we are incrementing so it will be reaching to the last one a and then again we are calling for this print after printing again we are incrementing and that time we are checking if that string is exist or not if no then it will be return to the main. So here what happened? We are having this stack in the stack it is storing all this character which we are working on. And immediately we are printing so putting the stack that is we are call as push in the stack and when we are printing it is popping immediately pop the character push the character n pop the character n because immediately we are having the print statement. Now again we will see the same program ok now we will see the head recursion in the head recursion what we are doing we are having the same program just we have seen but here we are having the this recursive function at the start instead of at the end we are having at the start. Now what will happen we will see here our program is as it is in the stack we are having this static variable India here now we will start here what is here in the function we are having if star s means our pointer is there still on the string then we will be after execution our pointer is here on the i because i is the first character then when we are coming here it will ask again increment the pointer ok we are incremented the pointer now our pointer is at n again it is telling to increment the pointer continuously then again d then i and then a. So we are in the stack as I said this is the program active stack is there when your program will start the stack will maintain all the contents of that so here continuously we are incrementing the pointers so our pointer will be reaching here a. So stack is working like this you are keeping books like on each other i is one book n is second book d is third book i is fourth book and a is fifth book now when you want to remove that books where your pointer is there that time it is possible to remove the top book only so this is called as a stack top of the stack and you can remove the top of the stack that is called as a popping when you are removing that it is called as a popping so then your pointer will be here at i again you are popped because you are printing that again your pointer is there so you are printing d again you are saying that print is there so your pointer is at n so it will print n then your pointer will reach to the i then it will print i now what happened because of the stack top of the stack we are printing the pointer will be at the top whatever it is showing or it is printing like this so whatever we are printing it happened reverse a i d n a india has become reverse and we have printed in this manner so these were two examples of the head recursion and tail recursion now here i am giving one example to you time and give me the answer this is the answer here i have asked in the question that we are having this string waltz and you have to find out the length for that we have wrote this function in this function we are having a integer c and then while we are having the string character by character it will go and it will count that with the recursion because once we are calculating w then we are incrementing one and again we are checking yes the string has complete or not then again a is there like this we are counting that recursively and then we are returning the c so in that eight characters and it will return the length so it is a eight references thank you.