1 00:00:00,740 --> 00:00:02,240 Biton supports recursion. 2 00:00:02,270 --> 00:00:06,200 So how to write the recursive code in Python we shall be discussing here. 3 00:00:08,480 --> 00:00:09,460 What is recursion? 4 00:00:09,500 --> 00:00:16,430 So let me explain that one at first, the process in which a function calls itself directly or indirectly 5 00:00:16,640 --> 00:00:22,280 will be known as the recursion, and the corresponding function will be known as the recursive function. 6 00:00:22,610 --> 00:00:27,740 So that means what within the function body, the function will be called itself. 7 00:00:27,890 --> 00:00:33,080 So that is a direct calling and whatever the intended calling from the function and that function will 8 00:00:33,080 --> 00:00:33,590 be called. 9 00:00:33,740 --> 00:00:38,110 But that function in turn will be calling the the calling function. 10 00:00:38,240 --> 00:00:40,270 So that is known as the indirect recursion. 11 00:00:40,430 --> 00:00:46,010 So whatever the case, the recursion is there and the respective function is called recursive function. 12 00:00:47,240 --> 00:00:53,390 Using recursive algorithm, certain problems can be solved quite easily, and with the fewer number 13 00:00:53,390 --> 00:00:56,940 of calls in recursion, there will be a best guess. 14 00:00:57,110 --> 00:01:01,070 So what does a biscuit's that means in businesses for certain inputs? 15 00:01:01,370 --> 00:01:06,330 Outputs are known to us, and that is known as a business just considered in case of fact. 16 00:01:06,350 --> 00:01:07,340 Well, calculation. 17 00:01:07,490 --> 00:01:13,580 The best guess is that if the input number is zero or if the input number is one, then the factorial 18 00:01:13,580 --> 00:01:14,360 value will be one. 19 00:01:14,360 --> 00:01:22,250 So that is a Becka's say in the subject calculation of two numbers that Becka's is that if the two numbers 20 00:01:22,250 --> 00:01:26,540 are equal, then the city is any one of the number itself. 21 00:01:26,720 --> 00:01:28,550 So that is known as a best guess. 22 00:01:28,730 --> 00:01:31,490 That means for certain inputs, outputs unknown to us. 23 00:01:32,090 --> 00:01:34,460 So Python supports recursion. 24 00:01:36,350 --> 00:01:42,420 So advantages of recursion, recursive functions make the code look clean and elegant. 25 00:01:42,680 --> 00:01:48,980 We know that in our data structure we had two algorithms for your two hours of problem, and we know 26 00:01:48,980 --> 00:01:54,440 that in case of recursive algorithm, we we wrote only four to five statements that are putting some 27 00:01:54,440 --> 00:01:55,730 non recursive algorithm. 28 00:01:55,790 --> 00:01:58,220 We had to write one page full of codes. 29 00:01:58,430 --> 00:02:02,410 So that means that recursive algorithms are very easy to write. 30 00:02:02,540 --> 00:02:03,880 The future course are there. 31 00:02:04,040 --> 00:02:10,540 So obviously that programmers will feel comfortable environment to write a function in a recursive version. 32 00:02:11,180 --> 00:02:16,710 So a complex task can be broken down into simpler problems using recursion here. 33 00:02:17,180 --> 00:02:23,240 So second generation is easier with recursion than using some nested iterations. 34 00:02:23,900 --> 00:02:26,520 So now what are the disadvantages of recursion? 35 00:02:26,900 --> 00:02:30,650 Sometimes the logic behind recursion is hard to follow through. 36 00:02:30,770 --> 00:02:32,330 So that is a very important one. 37 00:02:32,360 --> 00:02:37,910 So while writing the recursive algorithm must be having a good grip over the programming logic, other 38 00:02:37,950 --> 00:02:42,920 than sometimes the recursive algorithm will become too much tricky for you to understand the logic. 39 00:02:43,370 --> 00:02:51,260 Recursive calls are expensive, Insaaf inefficient as they take up a loss of memory and time with the 40 00:02:51,260 --> 00:02:54,200 depth of recursion is too big. 41 00:02:54,320 --> 00:02:59,390 In that case, obviously it will go on keeping the instances of the previous coding. 42 00:02:59,420 --> 00:03:02,630 So that's why the use memory recommended will be there. 43 00:03:02,870 --> 00:03:09,560 And that's why there is a one of the disadvantages of using a one recursive algorithm and recursive 44 00:03:09,560 --> 00:03:11,810 functions are hard to debug. 45 00:03:12,110 --> 00:03:15,940 So these are the different advantages and disadvantages of recursion. 46 00:03:16,100 --> 00:03:21,260 So let us go for one practical demonstration where we'll be showing you that how this recursion can 47 00:03:21,260 --> 00:03:23,800 be implemented using python coding. 48 00:03:24,050 --> 00:03:25,760 So here is the demonstration for you. 49 00:03:26,270 --> 00:03:32,220 In this particular demonstration, we are going to discuss recursion, how to implement recursion here 50 00:03:32,220 --> 00:03:33,730 to a defined one method. 51 00:03:33,740 --> 00:03:40,190 The name of the method is fact, which is taking this in as input parameter, if any, is less than 52 00:03:40,190 --> 00:03:41,780 equal to one return one. 53 00:03:41,790 --> 00:03:47,450 We know that in case of recursion, there will be some Becka's that is for certain inputs outputs to 54 00:03:47,450 --> 00:03:48,190 be known to us. 55 00:03:48,200 --> 00:03:55,220 We know that if the value of any digital or the value of any one, then factorial of N will be one here. 56 00:03:55,790 --> 00:03:59,960 Other is what will happen, return and start factoring in minus one. 57 00:04:00,530 --> 00:04:05,540 So let us suppose we have this value five here so n will be assigned to five. 58 00:04:05,540 --> 00:04:08,740 Five is less than equal to one is false. 59 00:04:09,110 --> 00:04:12,020 So it will go for five in two factorial. 60 00:04:12,020 --> 00:04:17,610 For now, factorial force of four will be assigned to and four is less than equal to one. 61 00:04:17,630 --> 00:04:18,770 The condition is false. 62 00:04:18,950 --> 00:04:21,990 So to go for four in two factorial three. 63 00:04:22,340 --> 00:04:26,000 So again, in this case we'll be having three and two factorial. 64 00:04:26,000 --> 00:04:27,020 Two again. 65 00:04:27,030 --> 00:04:29,320 Two is less than equal to one condition is false. 66 00:04:29,320 --> 00:04:32,210 So it will go for two in two factorial. 67 00:04:32,210 --> 00:04:35,560 One and N is less than equal to one will be true. 68 00:04:35,570 --> 00:04:37,690 So it will be returning the value one here. 69 00:04:38,030 --> 00:04:41,240 So that is my factorial, one that will get multiplied with two. 70 00:04:41,900 --> 00:04:44,900 So I'll be getting two here so that will get multiplied with three. 71 00:04:44,930 --> 00:04:50,620 So now this particular recursive call will retries back to the through the columns. 72 00:04:50,720 --> 00:04:55,520 And ultimately I'm going to get this one that is five two factorial four. 73 00:04:55,790 --> 00:05:00,620 So Factorial four will be obtained as 14 to three to two into one. 74 00:05:00,920 --> 00:05:04,000 So that that will be our twenty four into five. 75 00:05:04,130 --> 00:05:05,950 So 128 will be the outcome. 76 00:05:06,200 --> 00:05:07,640 So let us go for the execution. 77 00:05:07,640 --> 00:05:11,190 You can find that the factor of five is 120. 78 00:05:11,540 --> 00:05:15,560 So in this way, the recursive function can be written in Python. 79 00:05:16,280 --> 00:05:17,510 Thanks for watching this video.