video

Lesson video

In progress...

Loading...

Hello, I'm Ben and I'll be your computing teacher for this unit.

So in this unit, you're going to build up on all those skills that you've developed in programme with units one, two, and three.

And this unit we're going to focus on something called subroutines.

So, all you'll need for this lesson and this unit is a computer and a web browser.

You'll also need access to a Replit account.

So please do ask your parents or carers for permission before accessing and creating an account.

It's also really helpful if you have a pen and paper to make notes or if there's any other activities that you might just want to write down your answer to, okay.

So if you can clear away any distractions that you might have so you can really concentrate.

And when you're ready, let's get started.

Okay, so specifically in this lesson, you will be able to describe what a subroutine is, you'll be able to describe the purpose of parameters in subroutines.

You'll use procedures that accept arguments through parameters, and you'll be able to describe how subroutines are used for decomposition.

And then finally, you'll be able to list the advantages of subroutines.

So quite a lot we're going to cover that, but let's get started.

So let's start with a question for you, okay.

So first of all, I'd like you to make a prediction.

Now this is a risk free prediction.

It doesn't matter if you get it wrong but I would like you to draw upon what you have learned already and see if you can work out what this code does, okay? So, this is an opportunity for you to use your pen and paper.

If you want to just use your mind to just work out in your head that's okay too, okay.

But what I'd like to do is explain exactly what you think will happen when this code is executed.

Now, what you can do is make some assumptions.

If you want to, if it's helpful, you can assume that number one is inputted as 10 and number two is inputted as 15, okay.

So, pause the video for a moment and see if you can run through that code and see if you can work out what would happen if number one was 10 and number two was 15, okay.

So pause it and then unpause when you think you have an answer.

Okay, so what do you think actually happens? So let's have a look, okay.

Now I'm going to head over straight away to this code we're doing some little explanation at this point just to see whether or not you were correct.

So I'm just going to head over now you should be able to see this code, okay.

So this is the programme that you were looking at.

So I'm going to run it.

And I said that we put in the numbers 10 and 15.

So first of all, it's saying "Enter a number" and we can see that on line five here, okay.

So it says, enter a number there.

And we've also got, if I enter in 15, there we go.

And then entering a number again.

So I'm going to put in 10, okay, and then hit Enter.

What do you think was going to happen? Do you think there's going to be nothing that's going to happen? Do you think there's going to be something output? And if you think there was going to be something outputted what do you think was going to be outputted? Okay, so if that's it you're right, three, two, one.

It's said 15 plus 10 is 25.

Perfect, it did a nice calculation for me.

So let's now go back and explore that.

So don't worry if you didn't get that right.

So, if the code confused you a little bit because you've never seen code like this before in the units that we've done so far.

So let's explore it a little bit more detail.

Okay, so first of all, we were dealing here with what you call it a subroutine.

Now a subroutine is a sequence of instructions to perform a specific task with identifiable name.

So can you see what the identifiable name is? Okay, we will class come back to that.

Subroutines can be called upon whenever they're required.

So that just defines a subroutine, is not executed unless it's called, okay.

So we've actually used some really key language already here.

We've used what you call a subroutine, okay.

And that subroutine we've mentioned, performs a specific combined set of instructions, but that identifiable name here was called "Calculate".

So we can see that def just says we are now defining a subroutine.

I've got the word "Calculate" as the name of my subroutine.

Now this won't run.

If I were to type this into my integrated development environment and I have to type that into Python and hit "Run" then it won't look like anything has happened.

Nothing has actually been executed as such, okay.

Because it hasn't yet been called.

So what does that actually mean? Now, parameters and arguments.

Now, subroutine have what we call parameters, okay.

And we can see our parameters here because it's saying a and b.

So my subroutine has in brackets a and b and we know those as parameters, okay.

Now these allow values to be passed into subroutine.

A subroutine called, since we're actually going to call it, has things called arguments.

So now you can see on my code, we have these.

We said that when we put the def in there, you see here, we want to call the def and calculate a and b.

That's actually just defining my subroutine.

But you can see now I'm calling it by running the name calculate and passing into it, these things called arguments.

Now the values held in arguments are passed to the parameters, okay.

There you go.

So we say number one, 10, number five, and they are arguments and they are passed in as parameters, okay.

So the control flow differs when a subroutine is encountered.

So instead of running statements one after another, the programme transfers control out of line to the subroutine, and the code in the subroutine is executed, okay.

So you're with me so far.

Now, so far that's all very theoretical.

So let's just slow down a minute and let's step through this example, just a little bit more slowly and you can see exactly what's happening and hopefully I'll make a little bit more sense to you, okay? Okay, so as we've mentioned before, you can see from lines one to three, that is where we are defining our subroutine.

So we're not going to see an output on our screen at this point 'cause it's not yet being called.

So the first line that see our programme being executed is line five.

And you can see it says "Print " and we can see us on the screen here where it says input/output.

This is where I'm going to show you, I'll be true to our programme what the input and output results going to be.

So we can see, "Enter a number:" as appeared there.

So the next line is we say, it was asking for an input, my input is the number five.

So, the variable num1 is now five, okay.

And then we move to another print statement where it says "Enter another number:" And then we're going to enter in number 10.

So num2 is now 10.

So num1 is five, num2 is 10.

And now we get to the interesting part where it comes down to calling our subroutine.

So you can see that calculate num1, num2.

So calculate is going to call that some routine, I'm going to pass in to routine the variables num1 and num2.

And of course they are five and 10.

So thus I'm going to.

My program's going to look for my subroutine and then you can see the a and b are now being passed in the values that were linked to my variable.

So being passed in five and 10.

So a is now five b is now 10.

So that allows my next line to work, which is answer.

So as a new variable is going to be a plus b, that's five plus 10, and our answer has the value 15.

And it moved down to the next line which is to output the value which is a plus b equals answer.

So a was five, b was 10 and therefore answer is 15.

So we're going to see on screen, five plus 10 equals 15.

Okay, and that's a stepping through my first subroutine, okay.

So what I'd like you to do now is actually have a go at building your own subroutines.

So I'd like to head over to task one on your worksheet and to complete and create some routines that use parameters, okay? Don't be afraid to look back over the worked example that we've just done there and that's also on your worksheet and follow the instructions and see if you can do that yourself, okay.

So have a go thus follow the instructions.

And when you've done that unpause the video and we'll go through the solutions.

Okay, so how did you get on with that? So I said, when you started this activity that I would go to the answers.

So I'm just going to briefly show you my solutions to them.

Now yours might be slightly different.

I don't know, but either way, if you didn't get it don't be afraid to look at my solutions and then maybe pause the video so you can work out exactly how we've done that.

Okay, so the first one you were asked to do was this one which is the average numbers of routine.

Now let's take three of my parameters.

So you can see I've got a, b and c there, and to work it out I needed to add a, b, c together in brackets and then divide that by three to get the average and then to output the answer.

So what we need to do very similar to our worked example of two.

Taking the inputs but this time we're taking three inputs instead of two and then we're calling our subroutine.

That might be something that you forgot to do which was to call it at the end which was to call it by using the same name as you declared.

So I call my average underscore value, okay.

So I'm calling that there and passing into it the variables where my numbers are being collected from the user, okay.

So if I just put in.

I'm going to run this, just test it out.

So let's just put in, I input six, seven, eight is my input.

So six, seven, eight, okay.

What's going to be average of six, seven, eighth? Of course it's going to be seven, okay.

And you can see the average number is seven, perfect.

So that was the first one.

And the second one you were asked to do was to find the highest number between two values, okay? So this one, I'm taking two numbers, I'll it num1, num2.

So my subroutine I call highest and that's taking two parameters, a and b.

And then inside there I'm using selection.

So if a greater than b, well then the highest number must be a and otherwise the highest number must be b, okay.

So what I'm doing is I'm setting a new variable called highest number and set it to a.

If a is the highest set it to b, if b is the highest and then I'm outputting whatever the highest number might be, okay.

So let's run that and test it out.

So I'm going to type in.

Test out twice this time.

I'm going to put in 10 is my first number and five as my second number.

And obviously this should output 10 and there we go.

And let's tests out the other way round.

So I'm just going to put in five and 10 and it get hopefully, well that's quite a different number 40, okay.

So hopefully it show up for 40.

I thought that we're going to clarify.

So this has worked.

So, like I said, don't be afraid to just pause the video and take a little bit of time to digest and have a look at the code that I've written just in case you weren't able to achieve that, okay.

But let's move on either way.

So, using subroutines with decomposition.

So when we're designing a programme for specific tasks we can sometimes become overwhelmed by the complexity of it.

So decomposition is a skill that we can develop that helps us to break problems down into smaller, more manageable chunks.

So subrouitines definitely can help us decompose problems. So we can write a subroutine, to handle a sub-problem.

So part of that larger problem, okay.

So once you decompose the problem then you can work out the, take the larger problem to smaller, more manageable chunks.

So if you manage making each one of the smaller more manageable chunks into its own subroutine, it definitely helps with that decomposition problem, okay.

Now, once each sub routine has been created and tested, we can move on to other parts of the problem.

Let's look at that first subroutine that we created, the calculate subroutine that added two values together.

Now, once we created that subroutine we can then call it over and over again.

It's reusable to solve many more problems, so add more than one number at the same time.

So for example we calling the sub-routine three times there.

So calculate five and seven it's going to add those two numbers together.

The next line is going to add 12 and nine together.

And the next line is going to add 100 and 32 together.

So the output will be five plus seven equals 12, 12 plus nine equals 21 and 100 plus 32 equals 132.

So you can see that when we need to output the additional two numbers again, we just call a routine again rather than rewriting all the code needed to do that necessarily problem, okay.

So what I'd like you to do now is look at a larger problem, decompose that, work out what subroutines that you need to solve the problem.

So I'd like to head over to task two on your worksheet and decompose the problem given to you in the scenario.

Now, I only want you to complete steps one and two and then restart the video, okay.

So if I just head over to the worksheet just to show you what that looks like, okay.

So it is here.

So the scenario is this.

So a teacher would like you to create a programme that will help learners check that answer to a math quiz that they have been given.

Each question involves two numbers.

The two numbers could be used for addition, subtraction, multiplication, or division.

Here's some questions that learners were given.

So your job is to first of all decompose the problem and work out what sort of routines you think you might need to solve that problem.

And then what parameters and other things that you might need to consider when making them subroutines.

So just do those two problems, okay.

And then pause the video, do those two problems then unpause when you got to that stage.

Okay, so how do you get on with that? So hopefully you've worked out what sort of routines you need and other factors that need to be considered, okay.

So let's go through what I thought the answers were, but again decomposition isn't always a black and white thing.

It may well be that you've got slightly different to me, okay.

So the steps that I think needed or subroutines I think we needed, I think we needed an add subroutine, a divide subroutine, a multiply subroutine and they subtract sub-routine, okay.

So that's what I felt we needed.

And the other things I needed consideration for is that each one needed to use two inputs 'cause each time we're going to input two numbers, okay.

I think you need a prompt asking whether or not they would like to add multiply, subtract, or divide and then also you need some selection statements in there to pass numbers into the correct subroutine, okay.

So that's what I think was needed.

So hopefully you got something similar and if not you've got some answers there that you can work with.

So the next side for this problem is I would like you to now design an algorithm for the programme using pseudocode.

Now, if you're not familiar with pseudocode, that is a link on the worksheet, which takes you to a guide about how to write pseudocode.

So I'd like to write some pseudocode to how you think would solve the problem.

Then I'd like to write the code and see if it worked by testing your solution using Python, okay.

So I'd like to deal with that.

So head back over to your worksheet and you continue with the steps.

So pause the video, have fun making this programme, and then unpause when you've got solution.

Okay, so how did you get on with that? Okay, so hopefully you've got a working solution and if you've done that big well done, okay.

So what I'd like us to do now to finish off this lesson is think about what are the advantages of subroutines, why are we making them? So I'd like to pause just for a moment.

And maybe if you've got your pen and paper handy, maybe jot down some answers.

Why are we making subroutines? What can you see are the advantages of them? Okay, so pause the video, have a little think about that.

And when you've got some answers unpause the video and we'll go through through some of my answers.

Okay, so let's go through some of my answers then.

So the advantages of subroutines.

Well, first of all, it makes it easier to spot errors and fix them because you're dealing with smaller problems. It's a decomposition thing we were talking about.

One of the advantages of decomposition is that you are able to spot errors and maybe deal with those errors just with that one subroutine rather than try and work out what the problem is with a big, long, massive bit of code that might be difficult to debug, okay.

It also makes it easy to reuse the code because as we showed you, once we've made a subroutine we can call that sub-routine multiple times maybe solve the same problem, but with different parameters, okay.

Or different arguments, sorry.

Okay, so subroutine can be created and use with different applications.

It also allows you to decompose a problem in a structured format and a larger programming problem can be divided between many programmers, okay.

So we might have a bigger problem, and then once we decompose that, well we can get different programmers to write different subroutines that we then bring together in the same programme, okay.

So that is all for this lesson.

And if you've managed to achieve all the test today, you are able to code the solution to that problem, then big, massive, well done.

And I would really look to see the work that you've done.

So please do send me maybe a screenshot of your working solution or something like that, okay.

So if you'd like to share that with us please do I'd love to see it, then please ask your parents or carer to share your work on Instagram, Facebook, or Twitter, tagging @OakNational and using #LearnwithOak.

Okay, so I'm already looking forward to seeing you next lesson I hope you're looking forward to it and I'll see you then.