Python 3.5 : Global variable and parameter
Before moving on to the functions we are going to help you with my two functions and global variable above I explained to you that there were two kinds of variables we have the local variables and global variables. If we take a good look at the new function ask for age we can see that here inside there is a local variable that is only accessible inside the function if I change this function by putting age at this level I will change everywhere and even here age init I will change to age completely at this level and here I have instead declared this at the top I'm going to delete this and I'm going to delete that I'm going to run my code and you'll see your name titi here we can see that there is an error and we are talking about line 25 at the level of ask age the function call ask age so it's at this level that there is an error why? because the variable declared here is a global variable and for this function to have access to this variable it would be necessary to declare here Global age so this function must have access to the variable only if it is declared inside as a global variable I run my code What's your name? titi your age 23 see that the code works normally everything works normally but this method is not very advisable insofar as we would need to use this function to have the identifiers of several people, well we'll start again we'll take our starting function. The function that we had at the start we start again here and I'm going to show you with this function the importance of the parameters and we return here age init and at this level I've called it age like this Here we have a new start function we could call several people for that Put name 1 for the first person we copy this and we put name 2 for the second person as well as age age 1 for the first person and age two for the second person and at this level here we are going to put in parameter person and to call that here by asking the age we call directly the person like this and we are going to copy this make twice we change everywhere you see that it is good now we verify therefore this tele program to ask the name of two persons is the age of two persons. I'm going to throw your name it's normally for the first person that I'm going to put mimi what is your name flat second person you will put titi Sorry here we have to put name one and name 2 we run our code again What is your name mimi? your name titi is for the second one. mimi how old are you? 12 Titi how old are you? 34 you see you call yourself Mimi you are 12 next year you will be 13 and you call Titi you are 34 next year you will be 35 In conclusion global variables are variables that can be manipulated everywhere in the code and parameters are the elements necessary to make the code dynamic. Well, we'll see you soon for an emergency buoy on parameters.