Loading...
Hello, my name is Mrs. Holborow, and welcome to Computing.
I'm so pleased you've decided to join me for the lesson today.
In this lesson, we'll be exploring the difference between a global and local variable, and we'll create a programme which uses both local and global variables.
Welcome to today's lesson from the unit Programming: Subprograms. This lesson is called Global and Local Variables in Programmes.
And by the end of today's lesson, you'll be able to explain the difference between a local and global variable.
Shall we make a start? We will be exploring these key words in today's lesson.
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, e.
g.
within a subprogram.
Global variable.
Global variable, a variable that is accessible from all parts of a programme.
Look out for these keywords throughout today's lesson.
Today's lesson is split into two parts.
We'll start by defining the scope of local and global variables, and then we'll move on to create a programme using local and global variables.
Let's make a start by 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 global scope or local scope.
Global scope refers to a variable that can be accessed and modified from anywhere in the programme, even from inside subprograms. Local scope refers to variables that can be accessed and modified only within the block of code where they are declared.
Fill in the gap to complete the sentence.
The (pauses) of a variable is the section of the programme where the variable can be accessed and modified.
Pause the video whilst you have a think.
Did you select scope? Well done.
The scope of a variable is the section of the programme where the variable can be accessed and modified.
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? Maybe pause the video whilst you look carefully at the code.
Jun thinks the output from the programme will be 8.
Do you agree with Jun? Jun is correct.
8 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 5.
What will be the output of this programme when it is run? Maybe pause the video again whilst you have a look at the code.
Jun says, "The output of the programme will be 4." Do you agree with Jun? 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.
Time to check your understanding.
What will be the output of this programme when it's run? Is it A, 12, B, you'll get an error message because the value returned has been assigned to solution instead of answer, or C, solution? Pause the video whilst you have a think.
Did you select 12? Well done.
12 will be output because solution is a variable defined in the main programme that holds the value returned by the function.
Here is a subprogram that accesses a global variable and displays it as an output.
Here is a step-by-step execution of the programme.
On line 4, number is assigned to 5, a global variable.
So global number is equal to 5.
The example subprogram is called on line 5.
The programme flow moves to line 1.
5 is output because the number 5 is a global variable and can be accessed by the subprogram.
The programme flow then moves to line 6 and 5 is output, this time because number is available to the main programme.
So this programme will actually print out the number 5 twice.
Here is a similar programme, but this time the subprogram has been designed to update the value held by the variable number.
Here is a step by step of the programme execution.
Line 5, global number is equal to 5.
The example subprogram is called, the programme flow moves to line 1.
Local number is assigned to 10.
A new local variable is declared with local scope.
A global variable cannot be modified unless explicitly stated in the programme.
10 is output.
And then line 7 is run and 5 is output.
The value of the global variable has not changed.
To solve this problem, we can declare that the variable number inside the subprogram is a global variable, and we do that by writing the word global before the name of the variable.
Time to check your understanding.
What will this code output? Is it A, 10 followed by 5, B, 5 followed by 5, or C, 10 followed by 10? Pause the video whilst you have a think.
That's right.
This programme will print 10 twice.
That's because this time number has been defined as a global variable.
Okay, we're moving on to our first set of tasks for today's lesson.
And you've done a fantastic job, so well done.
For part one, explain the term scope in relation to local variables and global variables.
For part two, explain the difference between a local variable and a global variable.
Pause the video whilst you complete the tasks.
For part three, what will be output when the following code is run? Pause the video whilst you look carefully at the code.
How did you get on? For part one, you were asked to explain the term scope in relation to local and global variables.
The scope of a variable is the section of the programme where the variable can be accessed and modified.
You were then asked to explain the difference between a local variable and a global variable.
A local variable is only accessible within a specific part of a programme.
A global variable can be accessed anywhere in a programme.
For part three, you were asked to look carefully at the code and explain what will be output when the code is run.
This code will output 100 followed by 300, followed by 400, followed by 100.
Let's have a look at why that happens.
So on line 9, amount is initialised to 100.
On line 10 that's printed out, so we are printing out that first 100.
On line 11, we're calling the subprogram triple and we're passing amount to it, so our programme execution goes to line 1.
On line 2, amount is equal to n, which is the parameter we are passing to the subprogram, multiplied by 3, so 100 multiplied by 3 and then we're printing that amount out, which is 300.
We then return to line 12 of our programme where we call the quadruple subprogram.
So we go to line 5 and then line 6 where amount is equal to n multiplied by 4 this time.
So 100 multiplied by 4 is 400, and on line 7 we print that out.
And then lastly on line 13, we print out amount again.
Now because amount is not a global variable, it's still equal to 100, so it prints out the 100 again.
Okay, we are moving on to the second part of today's lesson, where we are going to create a programme using local and global variables.
Global variables are generally seen as bad practise by programmers.
This is because it can be difficult to track the value of a global variable if it is modified in other areas of the programme.
It's also harder to test a function in isolation if it accesses global variables.
Jacob says, "How do I make a variable accessible to the whole programme if I want to follow best practise and avoid global variables?" That's a really good point, Jacob.
Ah, Jun has a solution.
Jun says, "You could use functions and pass values using parameters." Parameter passing is the process of passing data into a subprogram.
It allows the values of a local variable in the main programme to be accessed and updated from within the subprogram.
Time to check your understanding.
What key word would you use in a Python function to pass a value back to the main programme? Is it def, return, or global? Pause the video whilst you have a think.
Did you select B, return? Well done.
Let's have a look at these two programmes.
Programme A uses a global variable.
Programme B instead uses a function which returns the variable A to the main programme.
Why should global variables be avoided if possible? Pause the video whilst you have a think.
Global variables should be avoided as it's 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 you can no longer test a function in isolation, which means test a function on its own without other parts of the programme.
Okay, we're coming onto our next set of tasks for today's lesson, and you've done a fantastic job so far, so well done.
This programme currently uses a global variable.
I'd like you to modify the programme so that it no longer uses a global variable but performs the same task.
Pause the video whilst you have a go.
How did you get on? Did you manage to remove the global variable from the programme? Let's have a look at a sample answer.
So the code on the right-hand side is the code with the global variable removed.
So in this case, we're using a return value.
So inside our function, which is called increase_score, we have a selection statement which says if answer is equal to yes, if it is, then a is equal to a + 1 and then we're returning the value of a.
Within the main programme, we initialise score to 0 on line 6.
We then set answer equal to yes, and then we say score is equal to and we call the function increase_score and we pass at the arguments score and answer.
And then lastly on 9, we print out the value held by score.
The programme has been amended to remove the global variable.
Line 8 now assigns score to the function call, passing the values held by score and answer to the function.
For part two, I'd like you to create a password checker that checks the password a user enters against a password held by the programme.
For part A, create a subprogram called password.
For part B, create a global variable called correct_password.
And then for part C, create a selection statement that checks if the entered_password is equal to the correct_password.
If they match, then the message Access granted should be displayed.
If they do not match, then the message Wrong password, try again should be displayed.
Pause the video whilst you have a go at the task.
How did you get on? Let's have a look at the solution together.
So on line 1, we're defining our subprogram, which is called password.
On line 2, we use a global variable and we define that as correct_password.
On line 3, we have a selection statement if entered_password == correct_password: print("Access granted") else: print("Wrong password, try again").
On line 8 of the main programme, we print the message Please enter a password and we store that value that the user enters as entered_password.
On line 10, we set correct_ password to Pass123.
And then we call the subroutine password.
For part three, amend your programme to remove the global variable but ensure the programme still performs the same task.
Pause the video whilst you have a go.
Let's have a look at the programme with the global variable removed.
So, the subroutine looks fairly similar, although this time we're passing in the values entered and correct.
So in the main programme on line 7, we ask the user to enter a password and we store that on line 8 as entered_password again.
On line 9, we set correct_password to Pass123.
And then on line 10, we call the function, but this time we pass it the values of entered_password and correct_password.
So this removes the need for that global variable.
Okay, we've come to the end of today's lesson, and you've done a fantastic job.
Let's summarise what we've learned together.
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 only within a specific part of a programme.
A global variable can be accessed anywhere in a programme.
I hope you've enjoyed today's lesson, and I hope you'll join me again soon.
Bye.