video

Lesson video

In progress...

Loading...

Hello, I'm Ben, your computing teacher for this lesson.

Now, this is lesson two of our programmer unit part four about subroutines.

So this lesson, we're going to explore all about things called functions.

So all you'll need for this unit is your computer or web browser, you'll also need access to your Replit accounts, so please ask your parents or carers for permission before accessing that account.

You'll also need a pen and paper to make notes as we go along through the lesson, okay.

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

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

Okay, so specifically in this lesson, you will explain the difference between a function and a procedure.

You'll be able to use trace tables to investigate functions.

And then you going to be able to use a function to return values in a programme, okay.

So to get us started, I want us to recap a little bit about something that we did in lesson one, okay.

So we talked about the advantages of subroutines, okay.

So I'd like to pick your brains a little bit and see if you can remember some of those advantages that we spoke about in lesson one.

So with your pen and paper, can you see if you can recall as many of those advantages as you can? So pause the video now, write down those answers.

And when you think you've reached your limits, then you can unpause the video and we'll go through the answers.

Okay, so what did you come up with? So let's have a look, see whether your list match my list.

So the advantages of subroutines.

Okay, so first of all, it makes it easier to spot errors and fix them because you're dealing with a smaller problem, okay.

It also makes it easier to reuse code, okay, rather than repeating those same blocks of code, we can just call it a function more than once, or subroutine, sorry.

The subroutine created can be used with other applications.

It allows you to decompose the problem in a structured format.

And you can use a large, sorry, a large programming problem can be divided between different programmers, okay.

So if you remembered all of those, then big well done.

Even if you just got one or two, you're on the right lines of trying to refresh your memory about what we learned.

So let's move on to the content of this lesson.

I'm going to start off by asking you to make a prediction, so don't put those pens and paper away just yet.

So I would like you to read this code that you can see on the screen.

And I would like you to just read through it and see if you can work out what would happen when this code is executed, okay.

So run through the code, trace through it, and then see if you can work it out.

And when you have an answer, then you can unpause this video and we'll continue.

Okay, so what did you think would have happened if I'd run it? Well, you don't need to wait too much longer to find out, because I'm just going to do a little bit of live coding for you.

So I'm going to go ahead and go over to this programme, I'm going to run it through and find out exactly what happened, okay.

So I'm just going to head over to the far right now.

Okay, so let's step into this programme now and have a look what it does and see if your prediction was correct, okay.

So first of all, I'm just going to run it.

Now, you predicted on a pen and paper what exactly you thought was going to happen.

So I'm going to run it and first of all, the first thing you're going to learn is whether or not you were correct or not, okay.

So let's run this.

Says enter a number, so I'm going to enter in let's say two.

Enter in a second number, three.

Right, so now we have an error, which is an interesting thing to happen, okay.

Now, you may have looked at the programme and thought that what this is meant to do, and you would have been right about what it's meant to do, it meant to actually work out the power of two numbers.

So two to the power of three is eight.

So it should have given me back eight, but it's not doing, we got an error.

So let's see if we can work out why that was an error, okay.

So we step into the programme and have a look at it.

This is one of my subroutines, okay.

So def to_the_power a, b, okay.

Answer equals a star star b, remember, we're using double star, that's to the power of.

If it's a single star, it multiplies them.

So the first line has been executed here, as we saw, is print enter a number.

We have a variable there, num1.

And the int input, so it's going to ask for an input, it needs to be accepted as an integer.

And then it stores the answer or links it to the variable num1.

And the same happens on the next line there, so it enters a second number and links it, the answer has been linked to a variable num2.

Okay, then what we do is we call that subroutine, okay, which is to_the_power, we're going to pass into it num1 and num2.

And that's going to do its thing up here.

But then what we're going to do is print the output.

Now, you'll notice that this is the error, okay.

So it says line 11 was the error.

So let's read the error message and not be put off by it, because error messages are designed to be helpful, even though it's written in red, it's not as angry as it looks, okay.

So we have here file main, line 11, that's the problem.

It says name error, name answer is not defined.

Interesting, isn't it? Okay, because if you look here, we have got a variable here called answer.

And we've also got the variable answer here, and we want to use it, but it's not letting me use it.

So why is that? Well, actually, the reason for this is because at the moment, when we put in a variable inside a subroutine, it only exists inside the subroutine.

Okay.

Now, so outside the subroutine, answer doesn't exist, we haven't defined what answer is outside that function.

And therefore, it doesn't know what to do, okay.

So at the moment, this isn't actually a function, this is called a procedure, okay.

Now, procedure is something like a subroutine that doesn't return a value.

So this doesn't return a value at the moment, okay.

But therefore, it's procedure.

But if we want to make it return a value, it becomes a function.

And let's just prove, first of all, that subroutine works, okay, and we can do that by, let's put print answer here, okay.

So let's run this again.

Enter a number.

Got rid of that bit, enter a number, I'm going to put two and three.

Right, there we go.

And you can see it has printed eight, but still having the problem.

So inside the subroutine, okay, we got a print, but that's not a return.

That's showing us something on screen, but it's not returning the value so it could be used outside of the subroutine.

So the simple way in which we can make it do that is including a return statement.

So what I'm going to do is I'm going to type in return, ooh, that return.

Return, okay.

And then I'm going to put answer.

So let's see if this works when I run this, because I'm returning answer outside of the function.

So let's see if this works.

Okay, so two, three.

Now, we're still having the same problem, okay.

Now, at the moment, answer still only exists inside my function, okay.

I'm returning a value, but I'm not using it anywhere, because answer still only exists inside there.

So if I want answer to exist outside of there, I need to do something about that, okay.

Now, to prove that it's now returning a value, I'm now going to put a print, ooh, not there, sorry.

Outside where I'm calling my function, I'm just going to put in a print and then it's going to run the function, I don't know why that there.

So I'm going to go print, so this will prove to me that it's been returned outside the function.

So I'm going to put in two, three.

And you see eight has appeared in there, but we've still got the error.

So the simple way to fix this is actually, you can see therefore it's being returned outside the function and it's been used here.

And rather than print it, we want to store it, okay.

Now, there's two ways in which we can do that.

We could simply type in the word answer, answer equals.

So answer equals, I have my variable now that exists outside of the function.

This is going to return answer, okay.

So when this gets called, it's going to return answer and I'm going to store it under a variable called answer that's going to be used there.

So this, in theory, fingers crossed, should now work, okay.

So let's run this.

Now, I want to say third time lucky, but I think I've tested this more than three times.

So I enter two and three.

And there we go, two to the power of three is eight, okay.

Let's test that with bigger numbers that I'm not going to know the answer to.

So I'm going to put in 16 to the power of 32, see what happens.

There we go, big number, okay.

The 16 to the power of 32 is that big number there.

Okay, right.

Now, just to kind of highlight something.

I've used the word answer a few times here.

Now, it doesn't need to be the word answer, just because I've used answer inside my function, I'm returning answer, it doesn't need to be answer in my programme later on.

For example, I could just call this result.

If I'm going to save this under the variable result, I need to change it here to result.

Because this is the value that's existing outside the function.

So answer's here, so answer's there, it's going to return answer, which is just a value, okay.

And then the value's going to be returned when the function is called, but I'm going to store it under a variable called results.

So let's run that again just to prove that that works.

And let's try a smaller number this time.

Four to the power of four.

Okay, four to the power of four is 256.

Okay, and you can see that works.

Perfect, so let's head back over to the slides now.

Okay, so now that we've looked at that, what I'd like you to do is pause the video.

And I'd like to head over to task one on your worksheet, where I'd like you to read the functions that you can see across the various slides and see if you can trace through the function, work out exactly what's going to happen and what will be returned, what will be printed and so on.

Okay.

So pause the video now, go to task one on your worksheet, then unpause when you completed the tasks.

Okay.

So now you've done that.

In the previous lesson, we looked at something called subroutines, okay.

Now, so far, we've already started introducing you to the terms procedures and functions.

So what we're looking at now is taking subroutines and making sure we understand the difference, how we categorise subroutines into either a procedure or a function, okay.

So let's have a look at a procedure.

And our procedure executes a specific set of commands when it's called, but it does not return a value.

Now, what we're looking at here is a screen, you can see that it prints a value, but it doesn't return something.

So as a function is used inside, used to calculate or process a value based on arguments that are given, but it always returns a value, okay.

Now, note, in Python, there is no distinction between a function and procedure.

No difference in the way that you write it as such, it all starts off with def and then a word, you can even take arguments and parameters, okay.

However, the difference is the procedure doesn't return something, it might print something, but it doesn't return it, okay.

Whereas a function will have that return a statement in it.

Okay, so it passes something outside of the function again after it's done its calculations or processes.

Now, it's important to note that when the return line is executed, the function will terminate at that point, okay, returning the value.

So any code that is written after that return is executed is completely ignored.

So if we look at that example on the right hand side, we've actually got a print statement after return answer.

But that would never be executed, because once answer has been returned, then that's been passed outside the function and the rest of the programme will continue executing, the function would break at that point, okay.

So here's some code for checking if a value is entered in an integer, okay.

So this is used to handle user input errors.

So what I'd like to do is you'll notice here, this is not a subroutine, there is not a functional procedure, it's just a section of code that handles user input errors.

So my question for you, and I'd like to pause the video again, is what value is input here and what happens to that value, okay? So what value is input here and what happens to that value? Okay, so pause the video, read through that code, see if you come up with an answer.

Okay, so I guess you've got an answer now 'cause you unpaused the video.

So let's go through the answer now.

Well, this programme itself allows for an integer to be input, okay.

Now, the value must not produce a ValueError.

Now, this programme handles that ValueError, so for example, it's saying, if you see my mouse, it says number equals int input.

Now, I put in something that's not an integer.

It raises this exception.

But I'm handling the exception, because it would raise a ValueError.

So if it sees a ValueError, what we're doing, we're handling that and we're saying you must enter a number.

And you'll notice that is inside the loop, so it will keep asking for them to enter a number.

And when they do enter in a number, then this line is executed, not_valid equals false, and we break outside the loop.

So while not_valid is broken because it is no longer true.

Okay, and it would stop, break outside the loop, okay.

So we can handle the input into a function and return the valid integer back to our main problem or another subroutine.

So here's another example of that code, but this time, it's being put inside a function, okay.

So we've got the same section of code, but this time, we've got this function here, okay.

And you'll notice that it has a return at the end of it, so it returns that number.

So once a function has been defined, we can call that function whenever we need the user to enter a valid integer.

And you'll notice that this function is being called twice.

We might even call it three times, okay.

We don't need to keep writing that same whole block of code three times.

We can just write it once, but we can call that function multiple times, and that might be useful to us depending on the programme that we're writing.

Okay.

So the point in case, okay, I'm going to show you this live coding, where we're going to take a really inefficient bit of code that's going to repeat lots of code over and over again and we're going to put it inside a function to make it more efficient, okay.

So if you would like to code along with me, you can either load up your times table programme from lesson 15, okay.

Alternatively, you can use this link that you can see on the screen here, and that'll take you to a starting point that you can code along with me.

It'll open in Replit, so if you want to put it under your own account, then you can fork that project and it'll stay under your account.

So I recommend pausing that video now, load that code, and then you can either just watch the video and do it afterwards or you can code along with me, okay, whichever you feel most comfortable with.

Okay, so go ahead and open that.

And when you read it, when you've got all that open and you've already started coding, unpause the video and we'll get started.

Okay, so we're looking now at a times table programme.

Now, if you took one of the previous three units, you may remember building this programme.

So we're just going to work on this a little bit more and work out how functions could apply to make our code a little bit more efficient, okay.

Now, if you didn't do this programme before, let me just give you a quick reminder about what it does.

So the idea is it's going to ask the user for a times table quiz, so you put in the times table you want to do and how many times you want it to run, okay.

So what times table you want to run up to.

And then it'll give you a quiz on the answers, okay.

Now, in fact, let's just run this to show you it.

So we'll click run.

It says enter a times table you would like to be tested on, so I'm just going to type in five, okay.

And the maximum value for your times table, three, that's going to ask me the quiz.

So one times five is five, two times five is 10, and just to prove it doesn't say correct every time, three times five is I'm going to put 11, which is clearly wrong.

And there you go, it says incorrect and the programme finishes, okay.

So that's the purpose of the programme, now let's explore this code little bit and actually work out what kind of error handling that we put inside this code was.

So we run through it, it says welcome to the times table quiz.

But then we have the thing called not_validated, and it says not_validated equals true.

And it says, while not_validated, try, print times table you'd like to be tested on.

It's going to ask for an int input, so it's going to make sure my input is an integer, okay.

And if it is, it's going to say not validated equals false.

And what that's going to do, it's going to break it outside the loop, okay.

So we got while not_validated, by default, that's true when it's actually been, that is true there.

By making it false, that breaks that condition and the programme moves on.

But if we put in something, so if an error appears and we put in an input that isn't an integer, we've got this exception here.

So except ValueError, so it generates a ValueError.

And we're handling that and say, print you must enter a whole number.

Now, because not_validated is, that line is not being reached here, it means that not_validated stays as being true, okay.

And therefore the loop, it stays in the loop.

Now, there are three points in which I'm being asked to enter numbers here.

And you'll notice that actually, if I highlight a line of code here, not_validated equals true, what's really nice about this software is any repeated lines, if you highlight one of them, it shows you where else it's been repeated.

And you'll notice that not_validated equals true is appearing three times in my code.

And what that means is I'm repeating that line of code three times and therefore, we look underneath, I'm also repeating this line of code and also the try is also being repeated.

So we can see that we're repeating these blocks not just one line of code, but we're actually repeating these blocks of code three times.

And often, when we're repeating lots of code, it should give us an indication that actually, maybe we're doing something that's not quite as efficient as it could be, okay.

So what I'm going to do here is that we can write a function that's going to, as we're running this section of code three times, we can write a function, just declare those lines of code once, but call that function three times, and therefore reducing the number of lines in my code and making the whole process more efficient.

Let me just show you what this code looks like if I run it again.

So I'm deliberately going to make some errors and just check that this code does work.

So enter a times table that you'd like to be tested on.

Now, we know that that worked beforehand, so I'm just going to type in a string this time and this shouldn't work.

Well, it shouldn't continue with the programme, more to the point, what it should do is it should raise this ValueError and handle it by saying, you must enter a whole number.

Enter.

There you go, you must enter a whole number, and it asks me to enter the times table again.

So just put in another string just to prove it.

But this time, I'm going to put a number in.

And you see the way it continues.

And again, show it doesn't work if I put a string again.

Keeps going through the loop, it won't break the loop unless I put a number.

And if I do put a number in, okay, an integer, sorry, a whole number, it will continue.

And the same here again, if I put in a number here, it continues, okay.

But if I put a string, it doesn't let me continue.

Right, okay, so let me just finish this programme.

I'm going to get it wrong every time, there we go.

So what we're going to do now together is we're going to write a bit of code now by adding in a function, okay.

So let's start off by putting in a function above where my first repeated line of code is, right.

So I'm going to put in here.

Well, I need to start off my function, can you remember what three letters I'm going to put in to start a function? No matter what the name of your function is, you start with three letters.

Can you remember? Okay, well, it is def, okay, so I'm typing def.

Okay, so I'm going to define my function, okay.

I'm going to call it valid_integer.

Valid underscore.

Valid_integer, there we go.

Finish it off with brackets, now, I don't need any parameters in this one.

Or not for the way I'm going to write it.

So put in colon, and then I need to start my code.

So I'm just going to have a look at what that repeated code is, okay.

Now, I'm going to take you through it step by step, I'm going to do it slowly.

But actually, an easy way to do this is just copy and paste the whole first function.

Also, not function, sorry, the whole first bit of repeated code and work out what you want to keep and what you don't.

But I'm just going to take it step by step so we can take it a little bit slow and really understand what's going on.

Okay.

So let's start off with that first line.

So not_validated is a line that's repeated three times, I definitely want that.

And I'm going to say not_validated equals true.

The next thing I'm going to do is enter into this loop, okay.

Because, again, they all do it.

So they all, if I highlight this again, you'll notice that it's repeated three times, so I do want that.

So I'm going to type in, oh, not there, yeah, there.

While not_validated, okay.

Now I need to try it.

This is a slightly tricky bit we got to now, they all included try, okay.

However, each one of the things that comes after try, we got these print statements here, enter maximum value, enter a times table, okay, and we've also got down here answer.

So they're all asking for numbers, but in a different way.

So either we don't include this print line and make, and try and code it in somewhere else, or we do have a print statement here but maybe make it a bit more generic.

So I'm going to go for that option, so I'm going to put print and I'm going to put in enter a number.

Seems to be about right to me.

Now, what happens if you do enter a valid number, okay? Well, not but yeah, we actually need to ask for the number, silly me, okay, so I'm just going to create a variable, I'm going to call it num equals int input, there we go.

So int input like that, perfect.

Now, the variable names are different, times_table, max_value, answer, okay.

Now, we can ignore this for now because we're going to use this and return it outside that function later on, this available, so it doesn't really matter that this doesn't match the names lower down.

So num equals int input perfectly.

And then if it is a valid number, I need to break out the loop, so I'm going to do that by putting not_validated equals false.

And that will break out the loop.

Now, if they don't enter in a valid number, it's going to raise an exception and that exception is going to be a ValueError, so I need to handle that just like I did lower down in the code.

So I'm going to put except ValueError, okay.

And what I'm going to do is I'm going to print, just like I did lower down, I'm going to say you must enter a whole number, okay.

So you must enter a whole number.

Perfect, okay, so really close to what we have below, okay.

But the difference is, rather than just carrying on now, because this is in a function, we need to then return that valid number outside the function.

So once they've entered a valid number, that number needs to exist outside of the function.

Because currently, it doesn't, it's just existing in this variable num, okay.

Now, I do need to use that, but I need to return it outside, okay, so what I'm going to do inside the function but not inside my loop, I'm just going to type in return num.

Let's put that in a new line so it's nice and clear.

Perfect.

Okay.

So let's now have a go at making sure this actually works, okay.

So I'm going to start off by just looking at this first section that's repeated.

So I can get rid of not_validated equals true, 'cause that's used up there.

I can get rid of while not_validated, 'cause that's used there.

I can get rid of my try, that's used there.

Now, the print is slightly different.

Enter a times table you'd like to be tested, now, I do need to keep that line.

So I'm going to keep that inside my, sorry, not inside the function, I'm going to keep that line but just make it an executable line outside the function.

Okay, now what do we do with this one? This one's an interesting one, times_table equals int input.

Now, we are using int input here, okay.

But it's a different variable name.

And we use times_table later on in the programme, okay.

So if I just highlight times_table, you'll see it being used elsewhere, so it's used down here.

So we do need that variable.

But what we're going to do is rather than get rid of the whole line, we're going to times_table equals, and at this point, we're going to call my function, okay.

So I'm going to put in valid_integer like this and that's going to call the function.

And therefore, when a number is being returned, that number being returned is going to be stored or held by the variable times_table.

Okay.

Perfect.

Right, so we can get rid of ValueError.

We can get rid of print a whole number, okay, and that's the first section dealt with.

Get rid of not_validated equals false.

Okay.

But let's see how this works, so obviously, we're only doing it for the first section, so we do need to do more work.

But at this point, I'm going to test it to see if it works, okay.

What do you think, you think it's going to work? You're feeling confidence? Good, I'm feeling confident too, okay.

So I'm going to run this and see what happens.

So it has worked without errors so far.

So it's saying welcome to the times table quiz, which is that line there, line one.

And then it says, enter a times table you'd like to be tested on.

So that line has worked.

Now it says enter a number, which means that it has entered my function.

So this means it has called my function, okay, my function is being run through and so far, we got to this line here, line seven, okay.

So let's enter our numbers here, in fact, no, sorry, let's test it out, I'm going to put in a string.

So I'm going to put in hello 'cause that's a string.

See if that works.

Perfect, it's saying you must enter a whole number, and I put another string, it should just continue looping through, 'cause the condition hasn't been broken.

Perfect, and now let's enter a whole number, I'm going to type in five.

Right, and there we go, and it's worked.

So enter the maximum value for your times table.

That's now got to line 23, which means, you can see it entered into my, we stepped into the function and it returned the value out and then continued with the programme.

So that's absolutely what we wanted.

So let's stop this programme from running now and continue adding in the function at the right place.

So I'm going to go through it line by line, so I don't want that line, okay.

I don't want not_validated.

I do want to keep the print statements, so let's just move this up here.

I don't want the try.

Now, I do want to keep the variable max_value, 'cause you can see max_value is being used lower down.

But I don't want this and just like I did before, as I did on line 17, I'm just going to put in valid, ooh, underscore integer and call my function again.

Excellent, we're really doing well now, let's get rid of those.

We want to get rid of the print.

Okay.

Now, we do want to keep the max_value.

Okay.

We do want to keep answer.

We do need that print line, we do need this loop, okay.

So we've got to be really careful when we do this bit, because now we're inside another loop.

So we want to make sure our indentation stays correct.

So let's go through it, we do not_validated equals true, that's the repeated line that we don't want.

I don't need this loop, so this is where we get slightly tricky to make sure that we are still inside this for loop.

So I'm going to get rid of that.

I'm going to get rid of this.

Okay.

I do want to keep the print answer, so I'm going to put that in line there.

Let's just delete some lines.

Okay.

Now, I do want to keep user_answer, but again, I don't want to keep this section and I do want to call my function, okay.

Valid_integer.

I want to get rid of the not_validated and I can get rid of my exception.

Okay.

So let's see if this works.

I think the selection is in the right place, the if statement there.

But we'll only know when we run, all right, so let's just have a look, so I'm going to run this.

Working so far, I've not got a syntax error, which is always a great start, okay.

So enter a number, I'm going to enter in five.

That's going to say, now, I haven't tested this out yet, so enter the maximum value, I'm going to just put a string.

Perfect, that seems to be working.

So let's say five times three, so I'm going to enter in three.

Looking good so far, so answer, enter a number, I'm going to type in five.

It says correct, perfect, if I enter in something that's wrong, I'm going to put in 56.

Incorrect.

And three times five is 15.

Perfect, okay, and we've done it, we've made that programme now.

And in fact, if you look now how many lines of code we've got rid of, okay, it's a much simpler programme.

And we're making it way more efficient by using that repeated block of code, putting it inside the function, returning the value outside of it, okay.

But each time, all we're doing, if you look at this, we're calling that function three times.

So reducing significantly the number of lines of code, and therefore, for us, this makes it a much easier programme to read and a lot more efficient to write as well, okay.

So perfect, okay, so let's head back over to the slides.

Okay, so now what we're going to do to finish off the lesson is I would like to match the keyword to the definition.

So again, can you pause the video now, have a look at the keywords on the left hand side and see if you can match them up with the correct definition on the right hand side.

Okay, so let's go through the answers, see how you got on with that.

So function, which one of the, which one was function? Okay, so there we go, so a function is a subroutine that returns a value, okay.

So let's move to the next one then.

So a procedure.

Now, a procedure is a subroutine that executes a block of code when called.

It does not return a value, okay.

That's the key learning goal from this lesson.

So a return value, what's a return value? Well, hopefully this was probably one of the easiest ones, is a return value is a value that's returned by a function.

But the keyword there is function, not procedure.

Now, parameter.

So a parameter, we got two left now.

So a parameter is used in a subroutine to allow values to be passed to them, and therefore, that only leaves us with arguments.

So an argument of value is held in the brackets of a subroutine call.

So these are passed into a subroutine by the parameters.

Okay, so hopefully, you got all of those, and if you did, big well done.

So that's all for this lesson, and the only thing left to do is complete the end of lesson quiz.

So take your time doing that, and it'll help you recap on some of the learning that you've done in this lesson.

So that's all, so I really hope that you've enjoyed it.

And I really hope that you're feeling a bit more confident with your programming skills, because we're building on all those concepts that we've learned in the previous three lessons.

But now we're looking at more professional ways and better ways in which we can code using these subroutines and functions and procedures, okay.

So I would really love to see the work that you've done in this lesson.

And if you've, even if you've applied some of the concepts that we've learned about with functions and procedures and you've applied them to previous coding projects, please do share them with me, because I would definitely love to see that, okay.

But if you'd like to do that, please ask your parent or carer to share your work on Instagram, Facebook, or Twitter, tagging @OakNational and using the hashtag #LearnwithOak, okay.

So that's all for me, I'm looking forward to seeing you next lesson, so bye.