- [Craig] In this video, we'll look at the difference between local and global variables
and how to use them correctly in a Python program.
(uplifting piano jingle)
So here on the screen now, we see two separate subroutines written in Python,
roll two dice and display score bonus.
They both declare their own local variables called X and Y.
They both then make use of those local variables within the appropriate subroutine.
As these variables have been declared locally, they do not interfere or conflict with each other.
Even though they share the same name,
they are four different variables that are totally independent of each other.
So why are local variables important?
We've noted that subroutines have their own local variables,
and local variables only exist while that subroutine is executing or running.
A local variable is also only accessible from inside the subroutine they are assigned.
By using them, you help to keep the subroutine self-contained.
Subroutines can be used as part of a bigger program
without the local variables conflicting with variables in other parts of the program
which share the same name.
This is memory-efficient because the variables only exist and, therefore,
only take up space in memory while that subroutine is running,
and the program therefore becomes easier to maintain and debug.
We have updated our two subroutines to make use of a global variable called score.
This variable has been declared and initialised to zero
outside of these subroutines in the main program at the start.
By adding the global word before the variable inside each subroutine,
it allows the subroutine access to this global variable.
When the code in each subroutine updates the contents of score,
it's updating the same single global variable.
You've essentially given access to a variable outside of the subroutine,
which normally it would never see or know about.
Now, although using the global keyword works inside a subroutine
and allows us access to the global score variable,
this is considered very bad programming practice.
If you did this in an exam, you would most likely receive marks for functionality,
but you might lose marks for solution design or good programming practices.
Let's look at a better method you can use for updating global variables
without having to use the global keyword.
We have updated several lines of code and added some new ones
to make this a better solution without using the global keyword.
Let's work through it now so you understand what is going on step by step.
So the main program starts down here after the subroutines.
There's a few lines at the very top of the program where I've imported the random library
and initialised some variables to zero.
But in essence, the main program starts with the line ret score, shown in green.
We can see it calls the function roll two dice
and it passes in a copy of the global variable score when it does this.
The global variable score, as I've said, has been copied
and can now be used internally by roll two dice as a local version.
The function executes as normal,
but it is now only updating local versions of all these variables including X, Y and score.
When we hit the appropriate return line,
we now return two sets of values.
We return the local copy of score and it ends up back in the main program variable ret score.
And then we return the local copy of either X or Y,
depending on which part of the if statement ran.
And it ends up back in the main program variable, high dice.
We now print out which of the dice was higher.
Now remember, the value came from the roll two dice function
and it was one of its local X or Y variables,
but we're not using those any more.
Instead, it was passed back out to the global variable high dice,
and we are using that version here.
We now update our global copy of the score variable
to become equal to the current value of ret score
plus the result which will be returned from the function display score bonus.
As we call display score bonus, we pass in again a copy of the global variable score.
And remember, the global variable score has been copied
and can now be used internally by display score bonus as a local version.
Again, this function executes as normal,
but it is now only updating local versions of all its variables.
This time, these include X, Y and bonus.
When we hit the return line,
we now return the local copy of bonus and it ends up back in the main program
and is used in the calculation to update the global version of the score variable.
The final line of the program prints out the current score
by accessing the contents of the global score variable and casting it to a string data type.
This version of the program is considered a lot safer.
There is nowhere inside the two functions where the code has access to any global variables.
We are controlling the information
by passing copies of global variables in as parameters when we call the functions
and when we're done,
safely returning those to global variables.
It can take you a while to get your head around this concept,
so step through this a couple of times, following the program flow carefully.
So let's recap what we know about global variables.
They're declared outside of subroutines
and they exist in memory for the entire time the programming is running.
Global variables can be accessed from inside subroutines with the use of the keyword global.
This is, however, considered bad programming practice.
There are some benefits to using global variables.
The memory usage is consistent and known in advance.
There's no additional execution time taken in reserving and returning memory.
But there are also considerable problems.
Conflicting with variables in other parts of the program which share the same name
makes writing programs in teams and testing more difficult.
It can be memory-inefficient because the variable exists and, therefore,
takes up space in memory for the entire time the program is running.
We are better off passing in copies of global variables
you want to use as parameters when calling subroutines.
So that's the theory content of the video covered.
We're just going to spend a little bit of time talking about the programming language subset.
If you've heard this before,
you can skip the rest of this video.
Otherwise, it's definitely worth a quick watch.
So the exam board Edexcel have released a document that they call the programming language subset,
which you can see here on the right.
This document specifies which parts of Python 3 are required
in order that the assessment can be undertaken with confidence.
Students familiar with everything in this document
will be able to access all parts of the paper two on-screen assessment.
The exam board will not go beyond the scope of this document
when setting you any questions in the paper two exam.
Any students who do successfully use more complex methods which are not included in the document
will still, however, be awarded marks.
Further to this, you will see here from the front of the exam paper
that you are actually going to be supplied with a printed and an electronic copy of the PLS
during your paper two exam.
Well, if this document outlines everything you need to know
in order to program in Python during the exam
and you can be guaranteed that you can answer all the questions
using all the techniques in this document for full marks,
then you might as well get used to using it now.
Ask for a copy from your teacher
and have it as a reference guide at all times throughout your GCSE course.
It will be invaluable.
(uplifting piano jingle)