Lesson video

In progress...

Loading...

Hello, my name is Mrs. Holborow and welcome to Computing.

I'm so pleased that you've decided to join me for the lesson today.

In this lesson, we are going to be exploring how we can develop programmes using a structured approach, and what the benefits of using this approach are.

Welcome to today's lesson from the unit, "Programming subprograms." This lesson is called, "Structured Programmes" And by the end of today's lesson, you'll be able to describe what is meant by a structural approach to programming and explain the benefits of this approach.

Shall we make a start? We will be exploring these key words in today's lesson.

Subprogram.

Subprogram.

A sequence of instructions to perform a specific task with an identifiable name.

Interface.

Interface.

Describes how a programme will interact with the main programme.

Identifier.

Identifier.

A name for a variable or subprogram.

Parameters.

Parameters.

Used in a subprogram to allow values to be passed into them.

Look out for these key words throughout today's lesson.

Today's lesson is split into three sections.

We'll start by describing structural approaches to programming.

Then we'll look at the advantages of the structural approach, and then we'll finish up by using the structured approach in a programme.

Let's start by describing structured approaches to programming.

When computer programming was first introduced, it was very complex.

Most programmers were academics at universities who studied for years to write programmes.

Most programmes consisted of a single procedure with many lines of code.

In the past, programming languages did not have tools like if statements, while loops or for loops to control the flow of a programme.

Instead, programmers used something called go to statements to repeat sections of code.

However, using too many go to statements made their code messy and hard to follow, which is why it was nicknamed spaghetti code.

To make programming more accessible, new programming languages were introduced that use structures such as if statements and loops.

So you can see we've got while and for loops.

And for selection, we've got if, elif and else statements.

This approach to programming is called structured programming.

In structured programming, a programme can be divided into a set of smaller sections called subprograms. Each subprogram is designed to perform one specific task clearly and effectively.

Ah, Jacob's got an example.

One example is a subprogram that calculates the highest number in a range.

That's a great example, Jacob.

Well done.

Fill in the gap to complete the sentence.

In structured programming programmes can be broken down into a set of smaller programmes called? Pause the video whilst you have think what the answer may be.

Did you select subprograms? Well done.

In structured programming programmes can be broken down into a set of smaller programmes called subprograms. When using the structured approach, you need to carefully plan your subprograms and decide how they'll work together.

A structure chart is used to decide the hierarchy for a programme.

Let's have a look at an example structure chart together.

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.

A structure chart can be created to break this problem down.

When splitting a programme into subprograms, think of it like breaking a big task into smaller jobs.

Each subprogram handles a specific part of the task, making the overall programme easier to understand and manage.

So here we have our dog walking programme and we've split it down into a number of smaller tasks.

Number of dogs, number of days walked, total number of walks, total charge and invoice.

The top box is the main programme.

The other boxes are the subprograms within that programme.

After creating the structure chart, the programmer decides on the interface for each subprogram.

The interface determines how data will be passed in and out of the subprogram.

The interface is made up of the identifier, parameters and return value for each subprogram.

So let's have a look at the example for total number of walks.

The identifier is a meaningful name for the subprogram.

So here we are going to call this subprogram, "num_walks".

The total number of walks subprogram will be identified as num_walks.

Time to check your understanding.

What would be a meaningful identifier for the number of days walked subprogram? Maybe pause your video here whilst you have a think.

Did you manage to come up with a suitable name? Well done.

I've used the example here of num_days, but if you have something similar and suitable, then that's absolutely fine.

Well done.

Parameters define the information that must be passed to the subprogram.

So what information will the subprogram need? For the totem number of walks, the subprogram is going to need total_dogs and total_days.

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

Parameters define the information that must be passed to subprogram.

These values are calculated in the individual subprograms and then passed into your walks via the main programme.

A return value is passed back to the main programme.

So for total number of walks, the return value is going to be total_walks.

The values will then be used to calculate the total number of walks, which can be returned to the main programme.

Okay, we've come to our first set of tasks for today's lesson and you're doing a fantastic job so far, so well done.

I'd like you to start by describing what is meant by using a structural approach to programming.

Pause the video here whilst you think about your answer.

How did you get on? Let's have a look at a sample answer together.

A structural approach to programming involves organising a programme into clear logical sections to make it easier to understand and manage.

This approach includes using subprograms, which are smaller parts of the programme designed to perform specific tasks.

It also involves using loops to repeat actions efficiently and if statements to make decisions based on conditions.

By combining these elements, a structured approach helps create programmes that are organised, easy to follow and maintain.

Remember, if you need to pause your video here to add any detail to your answer, you can do that now.

For the next part of the activity, I'd like you to complete the plan for the dog walking invoice programme by deciding on the interface for each subprogram.

Note, invoice does not return any values.

Pause the video here whilst you have a go at the activity.

How did you get on? Did you manage to create the interface for each subprogram? Great work.

Let's have a look at some examples together.

So for number of dogs we've used the identifier num_dogs.

There'll be no parameters for this, but it will return the total_dogs to the main programme.

For number of days walked the identifier is going to be num_days.

Again, there's no parameters for this, but it will return total_days to the main programme.

The total number of walks have been done for you, so let's move on to the total charge.

For total charge, the identifier is going to be total_charge.

The parameter is total_walks.

It's going to need to know the total walks in order to be able to calculate the total charge, and then it's going to return total_cost to the main programme.

For the invoice, the identifier is going to be invoice, and then we've got lots of parameters which are being passed to this.

So we have total_dogs, total_days, total_walks, and total_cost.

Remember that we said invoice doesn't have a return value.

Remember, if you've used slightly different identifiers, that's absolutely fine as long as they make sense.

Okay, we are now moving on to the second part of today's lesson where we're going to explain the advantages of using the structured approach.

Sam says, "I've made lots of programmes that use sequence, selection and iteration.

Why do I need to use subprograms?" Maybe pause the video whilst you have a think about Sam's question.

Lucas has a similar point.

Lucas says, "Yes, my programmes work correctly too, so why do I need to change what I'm doing?" There are lots of benefits of using subprograms when programming.

These include code readability, easier testing, easier maintenance, and faster development time.

Let's have a look at these in a bit more detail.

Code readability.

It's much easier to read small chunks of code than one very large programme.

By breaking a programme into smaller sections, it becomes easier to read and check through the code.

Easier testing.

It's much easier to test a subprogram that carries out a single task.

Having less code makes it easier to find and fix mistakes.

Easier to maintain.

If a change needs to be made, it can be made and tested in one place and the rest of the programme will not be affected.

This means you can update a programme without disrupting the main functions.

If the new changes don't work, the entire programme won't be affected.

Faster development time.

A large programme can be worked on by several programmers.

This means that subprograms can be developed concurrently, which means at the same time, speeding up the development process.

Lots of programmers work in teams, each taking a small part of a larger programme.

Time to check your understanding.

Which of the following is not an advantage of the structure approach? Is it A, faster development time.

B, easier testing.

Or C, reduced coding errors.

Pause the video whilst you have a think.

That's right.

Reduced coding errors are not a benefit of the structured approach, but it may be easier to find them.

Another benefit of using the structured approach to programming is that subprograms can be called multiple times, saving the amount of repeated code or go-to statements.

As Sam says, "Yes, we don't want to have spaghetti code!" No, we certainly don't, Sam.

A properly structured programme should have one entry point and one exit point.

Python can be used as a structured programming language, but it is possible to break this convention in Python.

For instance, using a break statement to end a while loop is a deviation.

In structured programming, a while loop should only end when its condition evaluates to false.

Take a look at these two programmes.

Which programme uses a structural approach? Pause the video whilst you look carefully at the code and have a think.

That's right, the programme on the right hand side uses a structural approach.

The programme on the left hand side doesn't because it's using a break statement.

Okay, we are now moving on to our second task of today's lesson, task B.

I'd like you to explain two advantages of using a structural approach to programming.

Pause the video here whilst you think about your answer.

How did you get on? Let's have a look at a sample answer together.

A structural approach to programme means that code is easier to read and understand because the code is split into smaller, more manageable sections.

Testing a programme that uses a structural approach is also easier as testing can happen on a smaller section of code and errors can be identified more easily.

Remember, there are some more advantages of using the structural approach, so if you have a slightly different answer, that's absolutely fine.

Okay, we are now moving on to the final section of today's lesson, and you've done a fantastic job to get this far, so well done.

We are now going to use a structured approach in a programme.

This structure chart shows the dog walking programme that will provide a weekly invoice for clients based on the number of dogs, the number of walks and the cost per walk.

To define a subprogram in Python, you first write the word def, DEF, followed by the interface.

So an example for the total number of walks subprogram would be def num_walks(total_dogs, total_days): So total_dogs and total_days are the parameters that are being passed to your subprogram.

To return a value in a subprogram, you write the word return followed by the name of the variable that is being returned to the main programme.

The main programme can only use a variable from a subprogram if it is returned.

So let's have a look at the example for total number of walks.

So we have def numb_walks, with the parameters total_dogs and total_days in brackets.

We have our semicolon, and then we have our indented block of code.

We have total under_walks is equal to total_dogs multiplied by total_days.

And then we are returning the value of total_walks to the main programme.

Time to check your understanding.

What is the correct syntax to define a subprogram in Python? Is it A, B, or C? Pause the video here whilst you have a think.

Did you select A? Well done.

Remember, DEF, or def, is used to define a subprogram in Python.

The dog walking programme should allow the user to enter the number of dogs the client has, enter the number of days they have walked the dogs, calculate the number of walks based on the number of dogs multiplied by the number of days, calculate the total cost based on the number of walks multiplied by four 'cause each walk is £4.

and then display the relevant invoice information.

So number of dogs, number of days, total number of walks and total cost.

Based on the plan for the dog walking system, which subprogram would be best to create first? Is it A, invoice.

B, total number of walks.

Or C, number of dogs.

Pause the video whilst you have a think.

that's right, number of dogs would be the best subprogram to create first.

That's because both the invoice and total number of walks subprograms require return values from other subprograms, so it's best not to create these first.

Okay, we're moving on to our final task of today's lesson.

Task C.

I'd like you to open the starter programme at oak.

link/dog-walking.

Using the interface plan from task A and the following requirements, I'd like you to complete the programme.

So remember, the dog walking programme should allow the user to enter the number of dogs the client has, enter number of days they have walked the dogs, calculate the number of walks based on number of dogs multiplied by number of days, calculate the total cost based on the number of walks multiplied by four, and then display the relevant invoice information.

So number of dogs, number of days, total number of walks, and total cost.

Pause the video whilst you go and complete your programme.

How did you get on? Did you manage to create your programmes? I'm sure you did.

If you want to have a look at a full solution, you can go to oak.

link/dog-walking-solution.

Here's the example on screen.

So here you can see we have five subprograms which have been identified in the programme.

The first one, num-dogs, asks the user to enter the number of dogs.

It stores that value as an integer, as total_dogs, and it returns that value to the main programme.

num_days is a similar subprogram, but this asks the user for the number of days.

The third subprogram is num_walks.

This one has some parameters, so you can see we have total_dogs and total_days.

This subprogram calculates the total_walks by multiplying total_dogs by total_days.

And it returns total_walks to the main programme.

Total charge is the next subprogram, and this takes one parameter, total_walks.

This calculates the total cost by multiplying the total walks by four, and it returns total_cost to the main programme.

The subprogram for the invoice takes in four parameters, so total_dogs, total_days, total_walks, and total_cost.

It then displays all of these values to the user.

Note that this subprogram does not have a return value.

On lines 25 to 29, we are calling the subprograms. So you can see here we have some sample output.

So we ask the user to enter the number of dogs.

They enter two.

We ask the user to enter the number of days.

They enter 10.

And then we display the invoice information.

So we have number of dogs walked.

Two.

Number of days walked.

10.

Total number of walks is 20, and the total cost of the invoice, which is £80.

Okay, we've come to the end of today's lesson and you've done a fantastic job, so well done.

Let's summarise what we've learned together.

In structured programming, a programme can be divided into a set of smaller sections called subprograms. Each subprogram is designed to perform one specific task clearly and effectively.

Using a structured approach to programming has many benefits, including ease of reading, interpreting, and testing code.

The interface determines how data will be passed in and out of subprograms. I hope you've enjoyed today's lesson, and I hope you'll join me again soon.

Bye.