Python 9.2: Recursive Functions

We have seen that a function can call Another formation to send a result but there are also the recursive functions already what is a recursive function it is a normal function like all the others We will make a small example and you will see, by yourself I will put def a banal thing ab the name of the function with a colon and inside we'll do a print my name is and we'll call the function ab I'll run my code and you'll see that it's displayed Look at the function ab calls the function print and it can also be called itself That's what we call recursivity, so here I'm going to call the ab function and I'm going to run my code again you see we have I name myself I don't name myself several. it gives an infinite loop Here we see that there is an error on line 7 In fact it tells us that the function has been called several times And as it was called several times it makes a break that's why at this level if we go down well We'll see for a moment that we never have no I'm called and until the end so it should be noted that the recursive functions are not there to remember in an infinite way They always have an exit condition that means that at some point We will remember the limit Put a stop to recursion we can imagine that we pass To this function the number of 2 here And inside the parameters I will put an n and inside the print Instead of this I will put n double side first n comma n To get the value I put a colon And in the recursive function we will do n times n And we will stop until it has reached a certain limit that's why at this level, I'll put a comma and I'll put the limit I'll put the limit like this is here I'll put ten thousand so ten thousand is the limit This is a mathematical solution and we can also use recursive functions for any program Here we have multiplied the number With itself Until we reach the limit and I will check this By doing an if if if n is greater than than the limit colon we do a return to exit so this is what will allow us to stop the infinite loop And here inside don't forget the limit parameter Otherwise it will give an error we run the code to see what happens you see that we have 2, then we have 4, then we have 256, that means that if we do 256 x 256 it exceeds the n which is one thousand and if we put 10.000 instead we raise It's all the same because the n times n , so 256 x 256 exceeds ten thousand I will put here one more zero so that it makes 100000 I raise you see that it gives so this was a first example concerning the calculations and we'll move on to a second example, imagine that we want to create a program Or a function that asks the user for a number from 1 to 4 so I'm going to comment this our first recursive function like this I want to create a new function that I will name request choice request dash choice Parenthesis colon And inside I'm going to do a print so we'll put answer Dash str equals input To get the user's choice Double rib What is your your choice I'll do a plus to concatenate str min parenthesis plus Double rib plus str parenthesis Max plus double rib And then I will convert the answer str into an int so answer dash int equals int and we will do a try at this level try colon I'm going to put this inside and here at the bottom I'm going to do an except except colon print double coast error you have to enter a number We had already done this above in the try we will check the number entered by the user at this level by doing an if so if the answer int is not between the min and the max less than or equal to to the answer Which is less than or equal To the max answer two points We're going to do a print I will copy this we will make an error you must enter a number between between comma min comma and comma max and we will return the answer int return response int like this and for the moment we haven't called the recursive function yet I'll run my code Nothing and it's not easy I haven't called the function yet I'll call the function I'll create a variable choice equal ask for the choice And that takes parameter 1 4 Display the user's choice by doing a print double side the choice of the user colon comma I retry And we are told that there is an error On line 24 because the start function We had not put any parameter so here we will put the min comma the max and I run my code again you see that there is no more error a number between 1 1 and 4 the choice is 2 I retry If I put anything you see that it puts us The choice none It enters in The error function in the except to improve this, this is where the recursive function comes in we will make a return request choice And inside we will put the min at this level which means that if we enter a number that is not between 1 and 4 it will ask again the number it will enter the print And then it will ask for the number again, just like in except if you enter a number or A value that is not a number It will ask us again for the number like this I'll re-enter I'll put anything you see that it enters and asks us for the number again here I'll put two Good answer and now if I want to raise And that I put 5 it enters it asks us again the number, I put 9 It asks us again the number every time I put anything It asks us the number again and I put 4 It's good, it comes out That's what the functions are for. and we'll see you soon for the next video containing the break and the return.