Python Institute PCAP – Functions and Variable Scope
- Introduction to Creating Functions
Functions to perform various things so far in this course. And I told you that we’ll get into the hood, under the hood details later about what a function is and how you can create one. Because I’m of the view that you should learn how to drive a car first before trying to understand how the engine works. And so that’s what we’ve been doing. We’ve been driving the car when it comes to functions. And now to understand functions at a better level and for you to be able to create your own functions, that’s, that’s what we’re going to dedicate this lecture towards. Okay, so let’s first start with the print function. And I want you to notice a couple of things. And these are going to be key in understanding any function that you create or utilize. You have the function name and it’s highlighted by the editor because we have syntax highlighting. And then it has these open and close brackets or parentheses rather. Okay? And in these parentheses we pass data. And that data is actually known as arguments. That’s a key word that I want you to keep in mind. Arguments you pass to a function arguments or data. And a function is supposed to do things with it. When you think of a function, think of it as something that performs an action, a function does something.
So this function’s responsibility is to print whatever data that we pass to it. So if I pass in the word hello, this will of course print hello to the screen. So there are other functions like type and we’ve seen some of them. Now to get a better insight as to what the function is internally, we can actually use something called the help function. And in the arguments to the Help function, we can pass any other function’s name. We don’t need parentheses after the function name that we just need to give the name of the function to the Help function. And what help is going to do is going to give us what this function is all about. Okay, so let’s run this and notice that it gives us the signature of the print function. And what a signature is, is basically a template of how that function is to be utilized. So they have the value, this is what we want printed to the screen. And then there are some other arguments that we have not looked at yet. And that is for example, sep.
Sep is an argument, it’s a kind of data and end is another kind of argument, file is another argument and flush is an argument. So these are all the different types of data that this print function is capable of accepting as arguments. And then it gives us some documentation. It says prints the value to a stream, yada, yada, yada. Don’t worry about those details for now. But it tells you what each of these arguments mean, what they’re supposed to be used for. And these are, as you can see, optional. These are optional keyword arguments. And we’ll talk about how you can create your own optional arguments. But that’s really the key that I want you to keep in mind. This help is of course, also a function that has open and close parentheses. And in there we can pass anything, any other function name. Now let’s create our own function. And so the syntax for that is we first type def, which stands for definition. We’re about to define a function. And so then we give the function name. And so we can say let’s make a function called Greet person. So greet underscore person.
And this underscore is a naming convention that’s used in Python a lot. So make sure when you have compound words like this in a given name, you separate them with an underscore. And then we have the open and close parentheses. And then you put a colon. Okay? You put a colon. And then when you hit Enter automatically, what’s going to happen is your IDE is going to kind of move in. It’s going to tab in. Notice we have all this empty space, the IDE. As soon as you press Enter after the colon, it moves to four or five spaces in or whatever you have your editor set to by default, I believe it’s four. Let’s just make sure 1234. And so four spaces in. And here is where we actually give the instructions of what a function is supposed to do and what are the instructions. That’s basically code that needs to be performed by a computer. And that’s all we’ve been doing so far, right? We’ve been giving instructions to the computer to perform various things. And so we’re going to give the instructions inside of this method as to what needs to be done. And so Greet Person should greet anyone, right? So we can just say print. And inside of here we’re saying print. And what do we want to print? Well, we could say hello there. This is a greeting like that, okay? And then when you hit Enter and you go backspace, that’s it, this function. Now notice there’s a plus sign.
You can click on it and it hides the details, the guts of that. And this is handy when you’re reviewing code. But no need to hide this code because there’s only a few. There’s just one line in there. But I want to show you that you can use that feature in this IDE. So this is the actual function definition, okay? And so now I can call this greet person function. All I have to do is type in Greet underscore person and then I use the open and close parentheses. That’s it. This is known as defining the function. Notice the death and the colon. And inside of this we have the actual code. And this is actually known as calling the function, just like we were calling the print function. We are now calling the greet person function. And so let’s hit run. And notice it says, hello there. This is a greeting. All right?
So the instructions that were programmed in the greet person function are being executed when we call the greet person function. Now, like I said, your code is going to be interpreted line by line. So it starts from the first line and then keeps going line by line, and then it does all of the operations. If I was to move this code, this calling of the function before it’s actually defined, and I’ve put it here, right here, notice immediately the editor is going to put a red curly line underneath it. And this is basically a hint that, hey, there’s something wrong here. And you’ll see, when I hit run, notice the error. It says name. Greet person is not defined because at the point where the editor was running, when it got to here, it tried to run it and say it didn’t know what greet person was, even though we defined it down here. The editor is not smart enough to know that this is defined later with the way we have programmed it right now. All right?
There’s something known as object oriented programming, and that’s a way of organizing your code in classes and modules, which we’ll get into later. And that gives us all the flexibility we need in terms of calling our functions wherever we like. But in this case, we cannot call this function before it’s actually defined. That’s illegal. You can never do that. Regardless of, however, your structure, your code. The interpreter actually needs the code first to be able to run whatever it is trying to run. So let’s move this back to where it belongs. I’m going to cut that out here and paste it down here. Let me just get rid of all these extra lines. So now notice the red line is no longer there. If I run this, it’s going to run perfectly fine. The error went away. Now, just like the print function, remember, we passed it a value and the print function would print it.
Well, that could do a similar thing with greet person. What I would need is to specify an argument name, okay? And so I can call that argument anything. And so I can call it value, for example, or Val or anything. Let’s just call it Gibberish just to prove a point, okay? The argument name is Gibberish, okay? That’s what I chose to call it. And so now when a person calls this function, now they need to they must pass in something in the arguments. If I hit run, notice is going to get an error because it says Greek person is missing one required positional argument. It expects us to pass in the value of gibberish, whatever that’s going to be. Okay? And so if I pass in a string, let’s say taz, for example, my nickname or whatever and I hit run. Now, notice there’s no complaint because I am passing in some value. I can pass in the number twelve. No complaint. I can pass in floats or any other type of object into this function. It’s not going to complain now because we are following the signature of whatever this function expects for it to be used as. Now what we do with the gibberish is an entirely different story. Right now we’re not doing anything with this argument. If I was to, for example, do something, I could actually maybe make this a more personalized greeting. I can say hello there and then I can give the person’s name. All right, so let’s try that. I’m going to get rid of that greeting part and just put a plus sign and say that gibberish is going to be included in the output of the print statement. And I’m just going to add a plus sign again, and in quotes, I’m going to put the exclamation point at the end. Okay? Now whatever gets passed to the Greek person function will take the place of Gibberish. And then Gibberish is going to be of course used in these instructions and we are specifying how it is supposed to be used. All right? And so let’s pass in, for example, my name.
And so I can say MTOs. All right, that’s my name. So let’s hit run and boom. Notice now this time it’s saying hello there, MTOs. So the gibberish variable or argument rather was utilized in this function. Now there is something called default arguments, okay? And so I can give a default value in case if someone decides to call this function without giving any data, without giving any argument into this, this function would still run. And I can say that by default, gibberish should equal your name. And so by default, if someone does not pass in a name to this Greek person function, it’s going to assign the text right here which is called your name. It’s going to assign that text to Gibberish and that’s going to of course be used as it normally is. And so let’s hit run. And notice this time it’s saying hello there, your name, okay, where I can change this to Buddy so that it doesn’t look awkward. So let’s hit run now. And it’s saying hello there, Buddy.
But if I pass in a name such as MTR is my name, it’s not going to consider Buddy, this is the default value. It’s actually going to override that value with the data that I passed in. And so let’s hit run. And now this time, notice it says hello there, Mtrs. So oftentimes it’s a good idea to give your arguments default values like this, so that when someone is calling that function, obviously it needs to make sense. If you give a default value that breaks your code or doesn’t, that would obviously be bad, right? So you’ve seen that you can’t concatenate strings with numbers before we went over that earlier when we were talking about data type. So if I give a default value, for example, the number four, right, if I run this code, it’s not going to cause any issue because we are passing in the word Mtrs. This is text and string data could be concatenated with string data.
So let’s hit run and notice it says, hello there, Mtrs. There was no problem. But the minute I get rid of this and leave it empty, now the default value is going to take over. And so what’s going to happen when we try to concatenate a string with an integer like this? It’s going to throw an error. Let’s hit run and boom. There you go. Notice it says type error can only concatenate string, not int. Okay? So what we would have to do is, of course, turn this into a string by wrapping it with quotes or I kind of briefly hinted to you this other function much, much earlier on called the STR function. I don’t know if you remember, but if you do STR around any integer, it turns that integer into a string. And so let’s hit run and notice now it’s actually printing four because we are invoking the STR function on it. And so this gets first executed. Remember when we’re calling this, the entire statement does not get run first. It works from the inside out, right? So this gets evaluated first.
It converts the four into a string and then the concatenation takes place. All right? So just a couple of things to keep in mind. I’m just going to leave this STR in here just in case, because, well, we’ll give a default value of buddy and so that if someone was to pass in the number 100, we don’t want this code to break. It’s going to sort of handle it in a more graceful manner. It’s going to convert that hundred to a string using this string function. And let’s run this and notice it saying, hello, there are hundred. Now, another thing that’s also very important is you should be documenting your functions in a production grade application, right?
Now, we’re not going to do any documentation because we are learning this, but basically leave comments in the code so that other people can read and understand what’s going on. And when you define a function like this inside of that function, you better have some kind of documentation, especially in a production grade application. So the way that works is inside of the function body. Now, this is known as the function body, okay? And it only has a one line body. But if I was to add some other stuff such as print hey there, and then add some other lines, this is all the body of the function. All of these instructions are part of this function. As long as they are spaced like this. You want to make sure. So this is the body of the function. When you run it, of course it’s going to run all of the commands that are in that function and it’s going to print to the screen whatever it is, whatever those instructions are saying. But along with these types of code instructions you want to provide comments to whoever’s going to be using that function to the person, the programmer.
And so the way we could do that is we put three quotes like this, either three single quotes or three double quotes and inside of here we can provide some information about what this function is and so that’s typically doc string and we can say information or anything. Actually I’m going to define this and say that this returns a greeting and then we can say that the input is actually the variable gibberish. That’s the argument name. This is actually referred to as a parameter, okay? In the function definition, this is the definition of the function. We are specifying that the parameter is this. But when you actually pass in the data this is known as an argument. Okay? So just a distinction in terms of the vocabulary and how to refer to these.
When you’re calling a function the data, when you pass it, that’s what is known as the argument. You’re passing an argument to the function but when you’re defining a function you specify that this is the parameter, okay? And this parameter gets this data. This is the default if you choose to have a default value. So when someone asks what is the parameter name for greed person, we can say that the parameter name is called gibberish and so gibberish is the input and then we can say output is hello name or something, right? It doesn’t really matter. You could just do something like that and this gives a general sense to someone that is going to use this function, an idea of how they can use it. We’re specifying that it accepts an argument which is called gibberish and the output you can expect to see a hello with a name or whatever.
Now obviously if we give this code to someone they’re going to see this code and know immediately what this does, right? But what happens in a real program and we’re going to get into that when we talk about classes, we’re going to be encapsulating these instructions into other modules or classes, okay? And so the programmer that would like to use your function may not see the code unless they actually try to click on Controlclick it and it will take them to the definition, right? Now Control Click is a good feature on the Mac. It’s called command click. So you press the command button first and you click on a given function name and it actually takes you to the built in libraries for that given function or the class where it’s defined and you can see what the body of that function contains. So the body of this greet function contains all of this code. But if we was to go to the body of this print function, let’s control, click or command click it and it takes us to the actual class. And this belongs in built in PY, okay? And notice it has the different arguments. And for sep, notice it has a default value. For end, it has a default value. For file, it has a default value. So regardless of whether or not you pass in arguments to the function when you call it, these parameters have already been given default values. And so that’s why also it’s mentioned that there are optional arguments here. Okay? And notice the documentation here. It’s using three double quotes.
You can either use three double quotes or three single quotes to write out the documentation for a given function. All right? And there’s a lot of other stuff in here, no need to go into the details just yet. But let’s scroll to the top and see that this is in a module. Notice there are some comments. It’s in a module called built ins, all right? And we’ll talk about what modules and classes are and so on later. So let’s close out of this. And so now what I’d like you to do is to define your own function, create your own function, call it remainder. And that function is supposed to return the remainder of dividing two values. So let’s say we give the value ten and we give the value three. Well, three goes into ten three times. And so what is the remainder?
The remainder is one. So that function is supposed to return one. Now, when you need to return a value from a function, you actually use something called a return keyword, okay? Now in this case, it’s not returning anything, it’s actually just printing to the screen. But you might be wondering, this is still returning a value, right? The difference between printing and returning is that you can capture a value that is returned into a variable or something. So if I was to, for example, instead of doing these print statements, if I was to just return this, all right, now what’s going to happen is it’s actually going to return this when this function is called? And so let’s run this, if I run this method the way it is now, if I’m calling it, let’s run it.
Notice nothing gets printed to the screen, but it is actually returning something. And how we can tell is I can save the value that this returns into some variable. And I could say greeting, that’s the variable name. And then now that I have the value saved into the greeting variable and that value is being returned from this greet person function, I can print that value by doing print greeting or doing anything I want with this value, okay? And you’re going to be creating your own functions that are going to return various values and you’re going to utilize the programming language to its maximum capacity by the end of this course. So don’t worry if this is tricky at this point. But anyway, we run this function, it returns the greeting because we are using the return keyword in the definition of this function and then we print greeting. All right?
So let’s hit run and boom, there we go. This is not being printed because of the instructions in the greet person. Okay, keep that in mind. The greet person function only returns the value. The thing that’s actually printing it is this command right here, the print function call. Okay? And this greet person function is being called down here and it returns, of course, whatever it’s instructed to and that gets saved in the greeting variable. And then we can print it right here. So I want you to do a similar thing. I want you as an assignment to create a function and you can give that function name remainder. And that function should have two arguments and it should basically give the remainder of dividing the first argument with the second one.
So for example, if ten divided by three, if you were to divide that, we want the remainder. And what is ten divided by three is remainder? That’s one because three goes into ten three times. So that gives us a nine. And then there’s one remainder from ten minus nine is one. And you’re going to have to utilize the modulus operator that I spoke about much earlier when we were talking about arithmetic in Python. You can utilize a modulus operator to get the remainder. And I’d like your function to be able to return that remainder of dividing the first and second values. So it’ll be the first value divided by the second. So try that out on your own and then you can resume to watch my solution. Okay, welcome back. Hopefully that wasn’t too difficult. I’m just going to remove this code here. We don’t need to print anything to the screen from the greet person.
Now I want to work on the remainder function so we can do def remainder. And as an argument to this, I’ll say that the first argument is number one and the second argument is number two. Now in this case, notice I did not give it any default value. If I wanted to give it a default value, I could do an equal and then give it the number twelve or whatever. But I’m not giving it any default value. I just want the parameter names to be NUM one and number two. Now after that we need a colon. And now we actually enter the method body and notice it’s four spaces in. Or you can type tab and it’ll take us four spaces in. And when you’re in this level here, this is where all of the code instructions would be of that given method. So let’s do NUM one, modulus number two, and we can say that this gets assigned to result like that and then we can return the result.
Okay? And so now I can utilize this function by calling it and we can say remainder and pass in ten as the first value and three as a second. And this will return the remainder. So we can say remainder value. This is the variable that’s going to store the remainder that’s returned from the remainder function and then I can print remainder value like that. So let’s hit run and notice it prints one. Now obviously this code could be shortened, okay, because we are capturing the remainder from running this calculation and we’re saving that into a result variable and then we’re returning that result. I could just do return on this line directly. So let’s just cut that out and paste it here and get rid of this line right here. And so now I’m just returning directly whatever this is going to equal and this is still going to run exactly the same. So let’s hit run and there we go.
And I can add comments to this function by either using three double quotes or three single quotes. And I could say that the input for this is NUM one and NUM two. And I could say that these are the numbers to divide to get a remainder with the modulus operator. It doesn’t really matter, you can really put anything in there. But at least we have some documentation here. Now I’m not going to be documenting a lot of the functions that we write in the course because that just takes too long. But this is just for the purposes of explaining to you that when you’re writing a production grade application at work, you want to make sure you put comments like this so that other people that want to run your functions know exactly how they can be used and what are the different arguments that they accept and what it returns and so on. So what are the key things to keep in mind when you’re defining a function? You use the def keyword and then you specify the name of the function. And here you provide any parameters that it may have.
And if you want you can provide the default value by using the equal sign for any of the parameters. And then you have the close parenthesis and then you need a colon. And this colon marks the beginning of this function definition. And so after this colon you press Enter. It’s going to take you four steps in and four spaces in and that’s where all of your code should be, all of the code that belongs to the greet person function. Now if I was to take this return statement and move it back like this, right, notice it’s complaining return is outside of the function. So we can’t do that. We need to be inside the function. If I have a, for example, print statement going on here, notice it’s giving us an error. It’s outside of the function definition. And so we need to move that in, all right? So all of the instructions should be inside of the function. And the way we regulate that is through tab or four spaces in. And all of the commands should be in that level or further in. And we’ll talk about what going further into a function means in terms of spaces later. But let’s get rid of that. We don’t need that print line statement. And so that’s the idea, okay? And when you’re calling a function, you better call it after it’s been defined. Obviously, I can’t call the remainder function before remainder is defined, right? Programs run procedurally. It runs line by line like this, right? So I have to define it before actually calling any function. All right? So these are the basics. Practice with these concepts. Create.
- *args and **kwargs in Python
So I’m going to show you a function that is part of the Python language that we haven’t looked at yet, and that’s called sum. And if we control click it, notice that the arguments here use these special words. There’s star ARGs and star star quargs. Okay? And this ARGs stands for arguments and quarts stands for keyword arguments. So this function is capable of accepting an unlimited number of arguments for ARGs and an unlimited number of keyword arguments for quarks.
And the way you pass that data to it, there’s a specific format. So how do you use this function with numbers? Let’s say we want to add numbers together. So the way we do that is we actually use a tuple. And inside the tuple we give all of the different values that we want summed.
So inside of these parentheses, I need another pair of parentheses like that. And then in here is where I pass in all the numbers that I want. So if I want to add 12345, doesn’t matter. And so what this is going to return is it’s going to return the result of adding all of those numbers together. And if I do print, I can print the result. And let’s hit run.
And there we go. You get 15. So how can I define my own function that can accept an unlimited number of arguments like this? Well, let’s actually define something. I’m just going to move this to the bottom and say def. And let’s define our function. I’ll call it my function, or something more creative, let’s say my sum, because this is my way of summing something and it accepts a star ARGs as the argument.
And we’ll go into how to utilize that in just a moment. Before I do that, let me actually change this to A-B-C and D, right? And if I return the result of A plus B plus C plus D, notice how limited this function really is. I can only use it for four numbers. So if I do my sum and I call it on 1234, this is the maximum number of arguments this method can accept.
And each of those arguments will of course be summed together to get the result. But the moment I try to let me get rid of this line. We don’t need to do that. The moment I try to add the fifth argument here and run it, notice we’re going to run into an error. My sum takes four positional arguments, but five were given. So positional arguments means each position here is an argument and that position is utilized in the code here, right? A is A here? B is B here? C is C there? And so on. But what is this five?
This is the fifth argument here. There’s no parameter that represents this fifth argument. And so that is why it’s crashing, right? We don’t have a fifth parameter. And so to fix this problem, I’d have to add another E and let’s just make it F and G. And pretty soon we’re going to run out of alphabets here and we’re always going to need the ability to add an unlimited number of numbers, right? So to make this more flexible and more dynamic and accepting of values and make this function more powerful, we want to give it the ability to accept an unlimited number of arguments, all right? And the way we do that let me get rid of this. The way we do that is we use the star ARGs here like that. So now what I could do is I can just return some of ARGs and this will give me a lot more flexibility.
Now I can run this, notice it compiles, there’s no errors or anything, but I’m not utilizing this function so let’s actually utilize it. Let’s now call my sum and pass in 1020, 31, 1110, five, six, whatever bunch of numbers here and this is going to return a value and so let’s save that value in a variable called result. And now I can print, I can print result. Let’s hit enter or run it and notice it gives me an 85.
So there is no restriction as to the number of arguments I can put in here. I can continue to give it data and I won’t be worrying about those positional arguments, right? So notice how flexible this star argues is I cleared the screen here to go over one more example. So in this prior example, we use this asterisk with the ARGs parameter name and this represents an unlimited number of arguments that would be passed to this mysum function whenever we use it, right?
Well, there is one more unlimited option that we have and that is for keyword arguments, all right? So I’ll show you an example and this type of a parameter is known as quarks K-W-A-R-G-S. So let’s define another function and we’ll call it key value funk, okay? And we’re going to use the quargs parameter name with two asterisks, okay? And we do Kwargs and notice the auto population did that.
Now this is going to take the place of multiple keyword arguments. I’ll show you what I mean. First, let’s print whatever quargs is going to be. So we’ll just write quorgs here. That’s all I’ll leave for now in this method and actually try to utilize it. Now, so how would you use a function like this? Well, you call a function like you normally would and then you pass in key value pairs, all right? The parameter name and then the value parameter name and then the value. So let’s say we have the parameter name and I can say the name is Mike. His, his weight is £200 and his age is 27. Okay? Now let’s run this and notice what gets printed. Notice it prints the key value pairs in a dictionary. So we’ve got the name key and the value mic. We’ve got the name weight and the value 200 and so on. So that’s what quargs is. And this type of a parameter is actually very handy if you want your function to be able to accept unlimited number of key value parameters.
So if you want to get all of the keys, you can do quorgs keys. And this is actually also a function and it’s going to print all of the keys when we run this. So let’s run it and notice it prints this DICT keys, dictionary keys, and then these are all the keys name, weight and age. If you want to get all the values, you could do values. And let’s run this and notice it says DICT values, dictionary values, mic, 227. And if you want to get the entire dictionary, you just print the quargs or return the quorgs or whatever you want to do with the dictionary. If you want to get a specific value based on the key, you could do get. So we could do quorgs get and pass in the key the parameter name rather, for which we want to get the value. And so let’s say weight, right? Let’s say I want to get the value of the parameter weight. We have to surround it with quotes. And so let’s run and notice it says 200. If I want to get age, let’s run that. Notice it gives us 27. If I give a parameter name that doesn’t exist in the call of this function, then it’s going to return none, meaning no value.
So let’s, for example, choose address. Now, address is not in a parameter that was passed to this function, so in this case, it’s going to return none. So let’s run and there we go. Notice it says none. Now, none is a value in Python. It’s an object rather in Python that represents nothing or the idea of nothingness. Okay, so there’s no value assigned to the parameter name address. It doesn’t exist in the call of function. If I was to add it and give some kind of address, then of course it’s not going to show none, it’s going to show the actual value. So you might be wondering, what’s the benefit of this? If you actually need to know the parameters that are passed in the body of this function, we actually need to know the parameter names.
What use is this? Well, there’s plenty of uses. You just don’t know enough about the programming language yet. But when we get into loops and iteration and recursion, you’ll be able to find all different ways in which you’ll utilize keyword arguments like this. You would be able to loop through all of the parameter names and their values and do things with them when we get into the looping section. So hold off on that thought. There’s plenty of things you could do with quorgs. Now, this quarks is just a naming convention for this given parameter name, we could change this to anything. Let’s call it John. All right. And now John will take the place of the variable and it’ll work exactly the same way. Notice it still runs. So Quargs was just a naming convention, which I recommend you stick to for the purposes of making your code easier to read by others. Very important. But just understand that you can name this anything. The keyword arguments for an unlimited number of arguments like this where we have ARGs with the asterisk this you’re restricted to using the reserved keyword ARGs. You need to use that specifically. But with keyword ARGs, you can name it anything. But I recommend highly to use the name.
- Basics of Variable Scope
Language. And this lecture is going to be on scope, variable scope. Now, we’ve been using variables, we’ve been assigning values to these variables, but we haven’t talked about scope. So what is scope? Well, I’ll explain to you in this lecture. So let’s start by defining a variable called age. And I’ll assign the value to be 27. And if I want to print, I can print the value of age. Now, let’s say later down the line, I’ve got a method called Increase age. And in this method, all it does is it assigns this age variable the number 30. My question to you is, is this age variable the same as the variable that’s defined out here? Now, this is inside of the scope of this particular method. Notice there’s nesting going on here, right? So this shows that this is in the body of the increase age method. But I want you to think about this. Is this age variable the same as the one that’s defined out here?
So let’s do an experimentation. I’m going to print the value age right here, right now. For obvious reasons, you should at this point be able to understand that when I run this, it’s going to print 27, all right? And if you have any confusion, obviously, without even knowing about variable scope, you’ll realize that I haven’t actually executed this method. I’m not running it. I’ve just defined this method. This method didn’t do anything. So as far as we’re concerned, at this stage, age is of course, only 27 because that’s the value that it was assigned. I’m printing it here. And even though I defined this method, I’m not running this method. So age is going to be 27.
So let’s run this. And of course, it’s going to show 27 printed twice. But here’s the thing. If I run this method by just invoking it like that now, do you think age will be 30? If you think that it’s going to be 30, then you’re incorrect, okay? Age is still going to be 27. And the reason for this is because this age is different than this age variable out here inside of the body. This method, this age variable is local to this method alone, okay? It has nothing to do with the age variable that’s defined out here. This age variable out here is known as global scope. It’s global to this script, so to speak, these lines of code here. And this is local to this increase age method or function rather.
So let’s run this and notice, of course, it shows 27 up here as a result of running this statement. And then it prints 27 again as a result of printing this statement because the variable age was not changed because the variable age here is different than the variable age up here. So that’s it for this lecture. I wanted to keep it nice and short. I’m going to expand on this concept a little bit in the next lecture, where we actually define a method inside of a method, where we have a nestation going on here, meaning we have this method defined, but inside of the body of this method, I define another method, right? And I’ll show you how to do that in the next lecture. And then you’ll be able to see how this age variable is being interpreted based on its scope. Okay? So once again, this has a global scope in this script, in this file. And then this has a local scope local to this method. Or, excuse me, this function definition this age has nothing to do with with this age. So let’s wrap it up. I’ll see you in the next lecture.