video

Lesson video

In progress...

Loading...

Hello and welcome to lesson five of our programming part for a unit called subroutines.

I'm Ben, your computing teacher for this lesson and this lesson, we're going to look at the structured approach to programming.

So all you'll need of this lesson as always is a web browser on your computer, obviously.

And also you need access to your rapid account.

So please do ask your parents or carers before login to that and then other than that, a pen and paper, it would be really helpful.

And then you can clear any distractions that you might have such as put your phone away and find a nice quiet place to work.

And when you've done all of that let's get started.

So in this lesson, you will be able to describe the structured approach to programming, you're going to be able to explain the advantages of doing it this way using a structured approach, and also what you're going to use that structured approach in a programming project of your own.

So let's start off by looking at the XOR function example that we exploreD back in lesson four, so in the previous lesson.

I'd like to look out at this bit of code that you can see there and answer the question.

So first of all, how many exit points does this function have? And if you think that's a really, really easy question, then move on to the explore question, which is how could you reduce the number of exit points? So pause the video, read the code and see if you can answer both of those questions.

So I think of your unpausing, 'cause now you've got an answer to at least one of those questions.

So let's go through the answers then.

So the first answer was it had three exit points.

Now remember a subroutine exits or terminates when it reaches the word return.

So three exit points is a bad thing here.

So the next question was, how could you reduce the number of exit points? Well, let's have a look.

So we could do that by introducing a variable to hold a true or false and return the value of the variable at the end of the subroutine.

So rather than just return, return, return, we store it with a variable and whichever part of that condition is met is going to return that variable.

So c can be false, false, or true, depending on which part of that selection statement it reaches.

And then we're only returning that value with the variable.

So that's much about the news and this return statements.

Now most modern programming languages are designed to follow the structured approach to programming.

So what's that mean? Well, our programmes use sequence selection, iteration, and subroutines to control the flow of execution as we've explored in previous lessons.

Now each line of code is executed line by line.

Now the flow of control can be transferred to a subroutine, that then will be passed back into the main programme when it terminates.

We've had plenty of experience doing that over the last two or three lessons.

Now to follow the structured approach to programming, we need to make sure that each block of code has one entry point and one exit point only.

Although Python can be used as a structured programing language, it is actually possible to deviate from it.

Now the starter activity demonstrated a programme that deviated from the structured approach by having those multiple returns, another common deviation is the use of a break in a while loop.

Now, although this programme will work as expected, it is not following the structured approach to programming.

And then the break is used to force the loop to terminate.

Now while loop should only terminate when the condition becomes false.

So we're using a break there, like it says there, to actually deliberately break it, when actually we don't want to deliberately break it using a break.

What we can do is just turn the condition to being false.

So how are we going to do that? So what I'd like to do is pause this, have a look at that code and think, how can you improve this piece of code so it does follow that structured approach? So you might just want to jot this down with a pen and paper.

It's only one line that you need to change, or maybe one or two, I don't know, see if you can answer that.

So not much you need to write down anyway.

So pause the video, write down your answer and when you've got your answer, unpause.

So let's go through the answer now.

Now I was right in change, we only need to change not one line, we only need to change that break to actually something that's going to split that condition to being false.

So by changing the break to not guessed equals false, the programme now has the correct entry and exit points.

And you can compare the two programmes on the following slide, which we will do, so there you go.

So these are the two programmes side by side.

So you can see the first one has two exit points by using the break, whereas the one that we want to use has one exit point because we're making that condition false explicitly in the set, in that way, by having that variable, not guessed and changing that to false.

So what I'd like to do now is I'd like to pause the video and I'd like to complete task 1 on your worksheet, where you are going to be asked to improve the blocks of code by removing those multiple returns and breaks from the programmes.

So through for you to work with as a worked example, to help you as well.

So we'll just copy and paste the code into a browser maybe if you want to, and then you can change the lines and text just to make sure it works and then copy and paste your code back into the worksheet when you've done that.

So good luck with that have fun, and then unpause the video when you caught solutions to those problems. So how do you get on with that? So let's now move to something called these GOTO statements.

Now, before structure programming was introduced, programmers would use GOTO statements to go to specific lines of code in the programme.

So you can see here answer equals userinput, if answer is equal to five, then go to line nine, else, go to line one, and then you've got your ENDIF.

So this meant that when a programme was executed, it would jump around to different lines of code, This made it difficult for programmers to trace code and detect errors and it created a thing called spaghetti code.

So here, the code has been represented as a flow chart.

And you might want to notice all these different lines coming off and how very quickly they imagine a bigger programme that's going to lots of different lines, that code could become messy really quickly and hence the spaghetti code.

Now, by following the structured approach to programmers, programmers could work with those blocks of code that we talked about before, okay, these subroutines, these could be independently tested and groups of programmers could work together on a larger problem by having their own blocks and subroutines to produce.

So using the structured approach means that you would need to carefully plan those blocks of codes and decide how they will work together, how they all their subroutines fit together into form while larger problem or one larger project, So structured chart is created to decide the hierarchy of the programme.

So let's actually work with an example to really highlight this.

Now, imagine a dog walker would like a programme that provides a weekly invoice for their clients based on the number of dogs, the number of walks and the cost per walk.

Now a structured chart can be created to break this problem down.

So here's an example.

Now the top box is the main programme.

So the main programme is we want a programme that's going to produce that weekly invoice.

Now the other boxes are those modules inside that programme, they're going to help us find that bigger solution or these modules we made up are subroutines that arrived the procedures or functions.

Now, after creating the structure chart, the programme of the sides on the interface for each subroutine.

Now the interface determines how data will be passed in and out as that subroutine.

Now the interface is made up of the identifier, so what the name of my subroutine is going to be, the number of parameters is going to take, so what these prompts are as well, and the order of the parameters and the return values for each subroutine.

So here's an example using, by looking specifically at the subroutine for the total number of walks.

So here my identifier, my subroutine name is going to be num_walks.

To calculate the total total number of walks it will need, number of dogs and the number of days walked.

So therefore I'm going to have my parameters, total_dogs and total_days, that's how I'm going to name.

Now to be able to get those, these values will be calculated in these subroutines and passed as values via the main programme.

So this obviously would take data from number of dogs and numbers of days walked.

And once we've done that, we need to then work out what it would return.

So the values that will be used to calculate the total number of walks, which will be returned to the main programme, and you can see I'm going to return total walks to the main programme.

So what I'd like to do now is you'll tend to have a go at this.

I'll start this off for you.

What I'd like you to do is go to Task 2 on your worksheet where you'll see the structure chart, just like you did on the previous slide.

But your job is to decide on the interface for each subroutine.

So read all the information you've been provided with, 'cause that will definitely help you create the structure charts, and then just complete the structure chart by putting in what parameters, if any, it's going to take.

And then what return values again, if any, each one's going to do.

So good luck with that and then unpause the video when you're done.

So hopefully you finished that, so this is my solution to it.

Now don't be worried if yours is a little bit different to this, it should be mostly the same, but you, of course, this is your programme so you of course might've used different names for these things.

So my identifiers here, you might want to pause the video and just check if you'd like, but let's just look at the number of dogs, for example.

So the number of dogs would have the identifier numbers_dogs It wouldn't be secondly parameters, but it would return total dogs.

So that's all that would take.

And then let's look at the invoice in the end.

So that will tap the identify invoice.

It would take the total dogs, total days and total cost, but in this case it doesn't need a return.

'cause I might just print it for example.

So there's my solution.

So pause if you want to just make sure yours is kind of ranked as well.

So if you then paused it, I'm assuming you're okay to carry on so let's move on.

Now, while deciding on the structure of the programme prior to hit's creation, it reduces the overall development time of the programme 'cause each separate team could be divided up between programmers as they will know what their block needs to do and the interface required for each subroutine.

So what are they you do now, as you probably have a predictable activity now, it's actually going to make this programme, right? So the programme that has started to make the solution for the dog walking programme.

So some of the code has been provided for you, but I'd like you to follow the instructions on top three of your worksheets to complete the programme.

So follow the instructions, make sure you read it, take your time, look back at was previous examples if you need to, that we've done in previous lessons as well.

That'll help you with this.

So good luck, but have fun with it as well.

And then unpause the video when you've got a solution.

So how do you get on with that? So hopefully you've managed to do that and you've managed to complete that working programme using your structured approach.

So if you've done that big, well done team.

Now let's finish up the lesson by looking at advantages of a structured programming.

So can you think of at least one advantage for using structured programming? So just have a think about that for a moment.

You might want to jot down some answers on a piece of paper if you'd like to.

So pause the video just for a moment, get at least one answer if you can, and then unpause the video when you've got that.

So let's go through the answers then.

So a bunch of structured programming, it can improve the quality of your code, ultimately, okay.

Decisions can be made through branching.

It stops the need for this kind of spaghetti code that we talked about with the GOTO statements.

It can improve the clarity of the code, it can reduce the development time, 'cause remember we're splitting it up amongst various different people.

And we've got really clear vision of how things going to marry up and it makes it easy to find and fix errors.

So well done if you've got at least one of those.

So that is all for this lesson.

And I hope again, you've got a big sense of achievement 'cause you're really developing your skills and really building upon those programming concepts that you know about already.

So next lesson, we're going to have a go at a project which is going to bring together all those skills that we've learned through our unit of our programming work.

So I hope you're looking forward to it and I'm looking forward to it and I'm looking forward to seeing you then.

So if you'd like to share your work with us from today's lesson, that'd be fantastic.

So please ask your parent or carer to share your work on Instagram, Facebook, or Twitter, tagging @OakNational and using the hashtag #LearnwithOak.

So I'll see you next time, bye.