# Lesson video

In progress...

Hi, am Ben, your computer teacher for the class.

So welcome to lesson three of our programming unit all about subroutines.

So this lesson in particular, we're going to explore the concept of scope.

So as ever all you'll need for this lesson is your computer and a web browser and you'll also need access to you about Repl.

it account.

And then also be really helpful looking out for pen and paper as well.

So other than that, if you can clear away any distractions that you might have, maybe turn your phone off and if you've got a nice quiet place to work, that would be really great.

Okay, so when you're ready, let's get started.

Okay, so in this lesson you will be able to describe the scope of variables.

You're going to be able to describe how parameters can reduce the need for global variables.

We're going to identify when we're going to use a global variables and also to be able to describe constants, okay.

So let's get going with that.

But before we do, I've got a set of questions to recap your memory from the first two lessons of this unit, okay.

So my first question is, if we look at this code on the left hand side here, what will be the output of this programme if we were going to run it? Now, you're more than welcome to pause that video just to digest this, perhaps I'll just give you a moment.

So you don't have to, if you don't want to, or do you think it is, do you think it's going to output? Multiply two four, then it's going to say eight.

Here's going to say six.

Or nothing because of a term value is not being held in a variable.

Okay, so what do you think it is? One, two, three, or four? So shout out the answer for me.

So three, two, one, it is.

Well it was actually, two, okay.

Because we run through it to have multiply two four, so you can see def multiply from line one.

The lines one to three is declaring my, and this is a subroutine.

But what type of subroutine is it? Is it a function or is it a procedure or hopefully you shout at me that it is a function because it has a return value.

So we can see it goes print, multiply at two, four, okay.

So that then runs my function.

There we go.

I pass this two and four into it.

So it's going to multiply two and four together, store under the variable answer, that's going to return answer.

And then I'm printing that straight away on line six.

So although there's no variable being declared, it's just printed straightaway.

So it's not stolen but it is using it, okay.

So next question then, what were the output of this programme? So really similar.

This time we're going to divide two numbers and it's slightly different.

Is it going to, if the output going to be.

Number one, divide eight, two, so that can be the output? Number two, is it going to output eight divided by two is four.

Number three, is it just going to be four.

And number four is an error message going to occur because the variable answer only exists inside the function.

So what do you think it is? Three, two, one.

It is for this time an error is going to happen because it is absolutely right in saying that the variable answer only exists inside the function, okay.

It only exists in that, although we're turning it.

When we call that function, we're not storing it under the variable answers.

That answer that only exists inside that.

So when we're trying to use a here, it doesn't know what it is.

That's going to be an error.

So next question then, this one's going to add two values together.

So what will be the output of this programme? Just give you a moment to think about it.

Is it number one? Is it 12? Is it going to output at eight, four? Is it going to be number three, which is, it's just going to say solution? Or is it number four an error message would occur because the value returned has been held in a solution, instead of answer Let's have a look, three, two, one, it is number one 12.

So this would have worked, okay.

So we look at line six that is having a variable.

That's going to run the, it's going to call my function, but it's going to return something, so that return is being stored in the variable solution.

And then if I'm accessing that variable, that's going to print the answer on line.

So well done it.

You got that.

So this lesson is going to be all about something called scope, okay.

So the scope of a variable, is the selection of the programme where the available can be accessed and modified, okay.

I'm just going to take a moment to let that digest.

But the best way to describe that is I'll be showing you this through examples, okay.

So we'll do that in just a moment.

Now, variables can either have a global scope or local scope, okay.

So global scope refers to a variable that can be accessed and modified from anywhere in the programme, even from inside the sub routines.

Now local scope refers to variables that can be only accessed and modified within the block of code where they were declared, okay.

Now not all programming languages handle scope in the same way.

Now, Python has been designed to discourage the modification of global variables and we find out why during this lesson, okay.

Variables are local unless otherwise declared, okay.

So by default variables will always be local, okay, unless we tell them we want them to be global.

Now a variable declared in the main part of a programme can be accessed globally by all subroutines.

It cannot be modified unless you explicitly state that the programme should be modified, so should modify the global variable, okay.

So I said that it's probably best if I show you this through an example.

So here we go.

So here's a sub-routine that is accessing a global variable and displaying it as an output, okay.

So let's just walk through this line of this code.

So we can see lines one, two.

That is just declaring my sub-routine, okay.

And you can see that is a procedure because it's not returning a variable, okay.

So we can see that we got the first line that actually starts at executing if line five.

So number equals five, okay.

And then the next line, it says examples of that's calling my procedure.

So therefore it's going to move into this.

And then the next line is going to print number.

Now we can see that it's accessing the variable inside my programme, okay.

And then it's also going to, once it's done that is then passing back into the main flow of the programme and we get to line seven and it prints number, which is five.

And the intention of this sub-routine is to modify.

So it's a slightly different one.

The intention of this subroutine is to modify the global variable number.

However, it doesn't execute as expected.

So let's run through this one then.

So line six, we've got number equals five, okay.

We then going to call my procedure, my sub routine, and then we've got number equals 10.

Now remember this number is a new variable declared with a local scope.

So global variable can not be modified, unless explicitly stated in the programme and we're not doing this here.

So this is a local variable that only exists inside this subroutine, okay.

So when it prints number on that line is going to print 10, okay, 'cause that's local to the sub-routine.

But then when it cause control flow back into the programme, we get to line eight.

It then goes to five.

So the value of global variables not being changed, okay.

Because this is a global one here.

This is a local one there.

And therefore when we pass back, it knows that this is that way round.

Therefore it's only good to print five.

So how do we get around that? So we wanted that to happen.

We wanted that to actually change the value inside our main programme, then later we can do it this way.

So here's a sub routine that will access and modify the global variable by adding global before the variable name.

So we are stating that this part of the programme should access and modify the global variable.

So let's step through this.

So starting on line seven, again, number equals five.

We then get to move inside my subroutine.

And this time we're saying, now we want you to use the global variable number, rather than using, creating a new variable inside locally, inside the sub routine.

we're now going to say, use a global numbers.

So the programme has explicitly stating that should modify the global variable number.

So now we're going to, this is now because global number number in my main programme have been overwritten and been stated as 10, okay.

So it's going to print 10.

And then when we pass control back into the main programme, we can see that that is also now 10.

So it's really worth noting.

Although we looked at a solution to the problem that, you know, used a global variable.

Global variables are generally seen as bad practise by programmes.

And 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.

And it's also harder to task the function in isolation, if that was access to global variables.

So if you are attempting to use global variables and think it's a good idea to try and then think carefully about whether or not parameter passing would actually be a better approach more from than not actually is the best way to approach it.

So what I'd like to do now is I'd like to pause the video and head over to Task 1 on your worksheet, where you're going to be asked to modify the programme.

So the idea of the task to convert the programmes that use global variables into functions that pass values via parameters, okay.

So there's a work sample for you to look out to, but other than that, you need to look at the vitriol programme.

Don't be afraid to just copy and paste it into your place and environment to then modify it and then paste your solution back into the worksheet, okay.

So good luck with that.

Have fun when you've finished up, I'll be here when you get back.

Okay, so now let's explore constants, okay.

So another method for holding and value in our programmes is called a constant.

Now a constant is a fixed value that doesn't change through the execution of the programme.

Now, constants can be helpful in a programme when you need to ensure that value have stays the same throughout execution.

And one example of this might be the value of PI.

So the value of PI is the value of PI.

You would never want to change that in your programme because well, it just stays the same as it is.

Now, not all programming languages deal with constants in the same way.

Now, in C for example, you need to declare a constant.

So for example, you can see the code that says const float PI equal 3.

14, okay.

Now that is telling the programme that is a constant.

And once you do that, then that value can't be overwritten by any other processes in your programme.

So the programme is instructed to change the value of a declared constant that it will produce an error.

Now, Python's a little bit different because Python doesn't allow or does not actually have a specific feature, for declaring constants.

So the only way in which we can do that is using a naming convention.

So all constants are declared that using capital letters.

So you can see the example that so PI in capital letters equals 3.

14.

So that indicates to the programme or to the other people looking at your programme, that you wanted it to be a constant, even though in theory and in practise, that could be overstepped, wouldn't produce an error, okay.

And it's highlighted here.

The Python language will still treat this as a normal variable and will not produce an error message at the value has changed on execution.

But the naming convention is used to let other programmers know that this variable is to be considered to be a constant, okay.

So if that makes sense.

So you can see that PI is equal to PI plus one that would add one to PI and therefore that would overwrite the value of PI.

Even though by using that convention, we're trying to almost asking people.

Now an example, where this could be used is for example, a game designer wants to ensure that all new levels begin with background movement at the same speed.

So the start speed should be the same for all new levels.

You can see that we've got a variable that we want to be used inside our programme.

But we do capital letters that say it's a constant, because all levels are going to start at five, okay.

So what we then might do with the start of equal level, the speed is set by assigning constant speed to a variable speed.

So we're changing it from a constant to available, which then can be edited later on in our programme.

So as each level gets harder, the speed will increase at different rates.

So the speed at the beginning of each level always stay the same, okay.

So you can see here, it says print go faster.

Yes or no.

So at that point, every point, yes.

Then we can change the speed and increase the difficulty.

Now the programme could, if you just assign five to the speed at the beginning of each level.

So this will remove the need for constant.

But why do we do it then? But if we think of, so what would happen if the programme had decided that the start speed it should be 10 instead, okay.

So what do you think? Well, they would need to go to every part of the programme that references the start speed and change each individual value to 10.

So what we're saying here is one subroutine, but then we have lots of different levels, 20 levels, 30 levels.

We would then need to go and change all those values individually, okay.

And by having a constant, the programme can simply change the value once and it will change the volume everywhere.

So what I'd like to do is have a go at the quiz where you can test your knowledge a little bit further and what we've learned so far in this lesson, okay.

So once you've finished this video, then just head over I'm please do, please have a go with the quiz because it really will help you try and remember some of the stuff that we learned and also try, and maybe identify some theories where you might need to go back and explore a little bit further, okay.

So that's all for this lesson.

And I hope you've learned a lot about scope and global variables and local variables as well as constants , okay.

So hope you enjoyed that and please do share your work with us.