Loading...
Hello, I'm Dr.
Das.
Welcome to Computing.
I'm so pleased that you have decided to join me for this lesson today.
In this lesson, we will be looking at global and local variables.
Welcome to today's lesson from the unit "Programming Subroutines." In today's lesson, we will be looking at global and local variables.
By the end of this lesson, you should be able to explain the difference between a local variable and a global variable.
Some of the keywords that we will be looking at in this lesson include scope.
Scope, the section of the programme where the variable can be accessed and modified.
Local variable.
Local variable.
A variable that is defined and visible for use only in specific parts of a programme.
Example, within a subroutine.
Global variable.
Global variable.
A variable that is available for use in all parts of a programme.
This lesson is split into two sections.
The first section looks at defining the scope of local and global variables.
The second section looks at creating a programme using local and global variables.
Let's start by looking at the first section where you will learn about defining the scope of local and global variables.
The scope of a variable is the section of the programme where the variable can be accessed and modified.
Variables can either have local scope or global scope.
Global scope refers to a variable that can be accessed and modified from anywhere in the programme, even from inside subroutines.
Local scope refers to variables that can be accessed and modified only within the code block where they were declared.
Right, before we move on, let's do a quick check.
Fill in the gap in the sentence below.
The dash of a variable is the section of the programme where the variable can be accessed and modified.
Pause the video here and see if you can work this out.
Did you answer scope? You were right.
The scope of a variable is the section of the programme where the variable can be accessed and modified.
Well done.
Not all programming languages handle scope in the same way.
Python has been designed to discourage the modification of global variables.
You'll find out why during this lesson.
Variables are local unless otherwise declared.
What will be the output of this programme when it is run? The programme is given in the image below.
Jun thinks, "The programme will output eight." What do you think? Pause the video here and have a think.
Jun is correct.
Eight will be displayed.
This is because the value held by answer is returned from the function and then printed by the main programme on line number five.
What will be the output of this programme when it is run? The code snippet is shown in the image below.
Jun thinks, "The programme will output four." What do you think? Why don't you pause the video here and have a quick think.
Jun is incorrect.
An error will be displayed because the main programme needs to access the variable answer, but it is only available inside the function.
Right, before we move on, another quick check.
What do you think will be the output of this programme, the programme that's shown in the image below, when it is run? Pause the video here and have a think.
I am sure you'll be able to work this one out.
Well done.
The answer is 12.
12 will be the output because solution is a variable defined in the main programme that holds the value returned by the function.
Excellent job.
Right, let's move on.
Here is a subroutine that accesses a global variable and displays it as an output.
In the next few slides, we will look at the step-by-step execution of the programme.
Let's start with line number four.
That is the main programme.
Over the next few slides, we will look at the step-by-step execution of the programme.
It starts on line number four where a variable number is declared and assigned the value five.
This is a global variable.
On the next line, the subroutine example is called.
The programme flow moves to line number one.
Within the example subroutine, the value of number is printed.
Five is the output.
The programme flow moves to line six, which has another print statement.
The output is again five.
Now, this is a similar programme, but this time, the subroutine has been designed to update the value held by the variable number.
Over the next few slides, let's look at the step-by-step execution of the programme.
It starts by declaring the variable number and assigning it a value five.
Number is a global variable.
On line number six, the subroutine example is called.
The programme flow moves to line number one.
Within the subroutine example, a new local variable is declared with local scope.
This is also called number and is assigned the value 10.
A global variable cannot be modified unless it is explicitly stated within the programme.
Line number three prints the value of the variable number and 10 is the output.
The programme flow then moves to line number seven where we find another print statement.
This time, the global variable number is being printed, so the output is five.
The value of the global variable has not changed.
But we really wanted, the aim of the programme was to update the value of the variable number.
So to solve this problem, we can declare that the variable number inside the subroutine is a global variable.
Notice the change on line number two.
The variable number has been declared global.
Right, before we move on, let's do another check.
What will this code output? The snippet of code is given in the image.
Pause the video here and have a quick think.
I'm sure you'll be able to work this one out.
Did you choose option C? You were right.
Both the outputs, both the print statements, line number four and on line number eight, will give the output 10.
Well done.
This brings us to the end of section one.
Well done on completing this.
Just to make sure that you have understood what we covered in this section, why don't you attempt this task? Task A has three parts.
The first part asks you to explain the term scope in relation to local variables and global variable.
Part two asks you to explain the difference between a local variable and a global variable.
Part three shows you a snippet of code and is asking you what the output will be when the code is run.
Why don't you pause the video here and have a go at working these three parts out.
You've been doing so well.
I am sure you'll be able to figure these out.
Okay, let's look at the answers for the three parts.
So explain the term scope in relation to local variables and global variables.
The scope of a variable is the section of the programme where the variable can be accessed and modified.
Did you get that one? I'm sure you did.
A local variable is only accessible within a specific part of a programme.
A global variable, on the other hand, can be accessed from anywhere within the programme.
Finally, for part three, did you get this sequence of outputs? In the first instance for the first print statement, the answer will be 100, then 300, then 400, and finally 100.
Let's look at this programme and see how we arrived at this solution.
So on line number nine, we start by declaring a variable amount and assigning it the value 100.
Line number 10 prints the value of this variable, so the output should be 100, therefore the first output is 100.
Then on line number 11, the subroutine triple is called with the parameter with the argument amount.
The programme flow moves to line number one.
Within the subroutine triple, another variable, this time a local variable, is declared and assigned the value n times three, n being the amount that was passed as a parameter from the main programme, which was 100, so 100 times three.
The print statement on line number three will output 300, as you can see in the solution.
The programme flow moves to line number 12, where another subroutine, this time quadruple, is called with the argument amount.
Programme flow moves to line number five.
Within the subroutine quadruple, another local variable, amount, is declared and assigned the value n times four, n being the parameter amount that was passed from the main programme.
Amount was 100, so n times four makes 400.
Line number seven prints the value of local variable amount, which is 400.
So the output of the programme should now be 400 as you can see in the solution.
Finally, the programme flow reaches line number 13 where there is another print statement and this time this print statement is printing the value of the global variable amount, which hasn't changed.
So the output will be 100, as you can see in the solution.
Did you manage to work all that out? Excellent.
You have done a brilliant job.
Right, let's move on and look at the next section where you'll be learning about creating a programme using global and local variables.
Global variables are generally seen as bad practise by programmers.
This is because it can be difficult to track the value of the global variable if it is modified in other areas of the programme.
It is also harder to test a function in isolation if it accesses global variables.
Jacob's worried.
"How do I make a variable accessible to the whole programme if I want to follow best practise and avoid global variables?" Jun suggests, "You could use functions and pass values using parameters." Parameter passing is the process of passing data into a subroutine.
This allows the values of the local variable in the main programme to be accessed and updated from within the subroutine.
Right, before we move on, let's do a quick check.
What keyword do you use in a Python function to pass a value back to the main programme? Your three options are given here.
Why don't you pause the video and have a quick think.
I am sure you know the right answer.
Did you come up with return? You were right.
Return is the keyword that you use in Python to pass a value back to the main programme from a function.
Shown here are examples of two programmes.
Programme A uses a global variable.
Programme B instead uses a function which returns the variable A to the main programme.
Right, a quick check.
Why do you think should global variables be avoided if possible? Pause the video here and have a quick think.
Global variables should be avoided as it is difficult to keep track of the value of a global variable if it is modified in different areas of a programme.
Global variables also make testing harder as the function cannot be tested in isolation.
Well done on getting that one right.
This brings us to the end of this section.
Well done on getting through this.
To make sure that you have understood everything that we covered in this section, let's attempt a task.
This task is split into three parts and we will do these parts one at a time.
So the first part shows you a snippet of code and says that this snippet of code currently uses a global variable.
It asks you to modify the programme so that it no longer uses a global variable, but performs the same task.
Why don't you pause the video here and give this a go.
The code snippet on the right gives you the solution to this part.
As you can see, the programme has been amended to remove the global variable.
Line number eight now assigns score to the function call, passing the values held by score and answer to the function.
Well done.
Let's look at the second part.
This part is asking you to create a password checker that checks the password a user enters against a password held by the programme.
Now, this part is split into few smaller parts.
You should start by creating a subroutine called password.
Then it asks you to create a global variable called correct_password.
Finally, create a selection statement that checks if the entered password variable is equal to the correct password variable.
If they match, the message "Access granted" should be displayed.
If they do not match, the message "Wrong password, try again" should be displayed.
Pause the video here and see if you can work this one out.
I'm sure you can.
Well done in attempting this.
So this here is a possible solution to this part.
You start by creating, you define a subroutine called password.
Within that, you create a global variable, correct_password.
Line number three implements the selection statement.
So if the entered password, which is stored in the variable enter_password, is equal to the correct password stored in the variable correct_password, then the output is "Access granted." Otherwise the output is "Wrong password, try again." Line number eight is the main programme.
It starts by printing "Please enter a password," so it's asking for user input.
The user input is captured and stored in the variable entered_password.
Line number 10 then assigns correct_password to be Pass123.
And finally, line number 11 calls the password subroutine.
Well done.
The final part asks you to amend your programme to remove the global variable, but ensure that the programme still performs the same task.
Pause the video here and give this part a go.
I'm sure you'll be able to work this one out.
So that is a possible solution for this part.
As you can see, we are still, so the main programme still asks the user to enter a password.
On line number seven, the input is captured on line number eight and stored in the variable entered_password.
Correct_password is declared and assigned the value Pass123 on line number nine.
And then line number 10 calls the subroutine password, but this time is passing parameters to the subroutine.
So the arguments that are being passed are entered_password, correct_password.
So these are the two values that are being passed to the subroutine password.
The programme flow moves to line number one where your subroutine password is defined with the parameters entered and correct.
The programme flow passes to line number one where your subroutine password is defined.
It takes the inputs entered and correct.
The programme flow passes to line number one where you define the subroutine password and this subroutine, as we saw, has been called with two values entered and correct.
Within the subroutine, the check happens.
If the variable entered is equal to correct, the output is "Access granted." Otherwise the statement, the print statement is "Wrong password, try again." Well done on attempting this task.
Well done on getting through both the sections.
The scope of a variable is the section of the programme where the variable can be accessed and modified.
A local variable is a variable that is accessible from only within a specific part of a programme.
A global variable can be accessed from anywhere in a programme.