Lesson video

In progress...

Loading...

Hi, my name is Dr.

Gus.

I am so pleased that you have decided to join me for the lesson today.

In this lesson, we will be looking at structured programming.

Welcome to today's lesson from the unit: Programming Subroutines.

This lesson is called Structured Programming, and by the end of this lesson you should be able to describe what is meant by a structural approach to programming and explain the benefit of this approach.

Some of the keywords that we will be using in the lesson today are: subroutine.

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

Interface.

Interface describes how a subroutine will interact with the main programme.

Identifier.

Identifier: a name for a variable or subroutine.

Parameters.

Parameters: used in a subroutine to allow values to be passed into them.

This lesson is split into three parts: Describe structural approaches to programming, Explain the advantages of the structural approach, and Use a structural approach in a programme.

Let's start by looking at the first section: Describe structural approaches to programming.

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

Most programmers were academics in 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 GOTO statements to repeat sections of code.

However, using too many GOTO statements made the 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 used structures such as selection statements, for example: if, elif, else, and loops, for example: while and for.

This is shown in the figure below.

This approach to programming is called structured programming.

In structured programming, a programme can be divided into a set of smaller sections called subroutines.

Each subroutine is designed to perform one specific task clearly and effectively.

Jacob says, "One example is a subroutine that calculates the highest number in a range." Let's just quickly recap before we carry on.

Fill in the gap to complete the sentence.

In structure programming, programmes can be broken down into a set of smaller programmes called.

Why don't you pause the video here and have a think.

You're doing so well.

Did you say subroutines? You were right.

In structured programming, programmes can be broken down into a set of smaller programmes called subroutines.

When using the structured approach, you need to carefully plan your sub routines and decide how they will work together.

A structure chart like the one shown in the figure is used to decide the hierarchy for the programme.

For example, 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.

Let's see how.

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

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

So if you were to look at our dog walking weekly invoice, it can be split into smaller tasks, like how many number of dogs did we walk? How many number of days were the dogs walked for? The total number of walks, the total charge, and invoice.

The top box is the main programme.

So your dog walking weekly invoice becomes your main programme.

The other boxes are subroutines within that programme.

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

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

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

Let's look at these in a little more detail.

The identifier is a meaningful name for the subroutine.

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

What would be a meaningful identifier for the number of days walked subroutine? Why don't you pause the video here and have a think.

A possible answer could be num_days.

Did you come up with something similar? Parameters define the information that must be passed to the subroutine.

So for our total number of walks subroutine, we could have two parameters: total_dogs and total_days.

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

So those two become our parameters.

A return value is passed back to the main programme.

Total_walks could be a return value that the total number of walks subroutine passes back to the main programme.

The values that have been passed as parameters to the subroutine, so in our case, total_dogs and total_days will then be used to calculate the total number of walks, which will be returned to the main programme.

Right.

Let's just recap what we have learned until now by doing this task.

Task A is split into two parts.

The first one says: Describe what is meant by using a structural approach to programming.

Why don't you pause the video here and have a go at answering this question.

You are doing so well.

I am sure you know what the answer is.

Pause the video here.

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 subroutines, which are smaller parts of the programme designed to perform specific tasks.

It also involves using loops to repeat actions effectively, 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.

Did you get that? I'm sure you did.

You are doing really well.

Right.

So part two of Task A is complete the plan for the dog walking invoice programme by deciding on the interface for each subroutine.

Now remember, we did talk about the total number of walks subroutine and what that subroutine would use as an identifier, as its parameters and as a return value.

Can you do the same for each of the other subroutines? So the number of dogs subroutine, the number of days walked subroutine, total charge and invoice.

Pause the video here and have a go at this.

Right.

I'm sure you've got all of those, but just to recap, the number of dogs subroutine could have an identifier, num_dogs.

Its parameters? Nothing really.

You don't really need to pass anything to the number of dogs subroutine.

The return value, however, will be total_dogs, so the subroutine will return how many dogs there were.

The number of days walked subroutine has an identifier, num_days.

Just like the number of dogs subroutine, this does not need parameters to be passed to it.

The return value would be the total number of days that have been walked.

So it could have the name total_days.

The total charge subroutine could have an identifier total_charge.

This one would need parameters, would need the parameters total_walks, which has been passed to the main programme by the number of days walked subroutine.

The return value for this subroutine would be total_cost.

The invoice subroutine will have four parameters that will be passed to it: Total_dogs, so the number of dogs, total_days, the number of days walked, total_walks, the total number of walks that was returned by the total number of walks subroutine, and the total cost.

So total_cost was the value that was returned by the total charge subroutine.

Well done.

Let's carry on now and look at the second part of this lesson.

Explain the advantages of the structural approach.

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

Why do I need to use subroutines?" Lucas agrees.

"Yes, my programmes work correctly too, so why do I need to change what I am doing?" There are lots of benefits to using subroutines when programming.

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

Code readability: It is much easier to read small chunks of code than one very large programme.

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

Easier testing: It is much easier to test a subroutine 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 that you can update a programme without disrupting its mean 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 subroutine 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.

Right.

Let's do a quick recap before we move on.

Which of the following is not an advantage of the structural approach? Why don't you pause the video and have a go at this.

Did you come up with the answer reduced coding errors? Well done.

Another benefit of the structural approach to programming is that subroutines can be called multiple times, saving the amount of repeated code or GOTO statements.

Sam says, "Yes, we don't want to have spaghetti code." Of course we don't.

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

Python can be used as a structure programming language, but it's possible to break this convention in Python.

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

In structure programming, a while loop should only end when its condition evaluates to False.

Can we check that? Which programme uses a structural approach? So you have been given two examples of code.

Can you just have a quick look at these two and decide which of the two use a structural approach? Pause the video here if you need to.

I'm sure you got that one right.

It is the second one.

In the first programme, the while loop has been forced to stop by putting in a break statement.

In the second programme, the while loop only stops.

So you come out and the programme comes out of the while loop when the not_guest value is False.

So you make the condition False.

Well done on figuring out which of the two programmes was the right one.

Let's just quickly make sure that you have understood what we have covered in this section by doing this task.

Explain two advantages of using a structural approach to programming.

You're doing so well that I am sure you know how to answer this question.

Why don't you pause the video here and have a go.

A structural approach to programming 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.

Is this something similar to what you were thinking? Well done.

Let's move on and now look at the final section of our lesson.

This talks about using a structural approach in a programme.

Let's delve into this a little more.

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 subroutine in Python, you first write the word def, D-E-F, followed by the interface.

For example, the total number of walks subroutine can be written in python like so.

def num_walks (total_dogs, total_day): and end this with a colon.

So def defines the subroutine.

Total number of walks is our subroutine.

Its identifier is num_walks.

So you say def num_walks, open brackets, and now we put in the parameters, total_dogs and total_days.

To return a value in a subroutine, 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 subroutine if it is returned.

Within the subroutine total number of walks, we calculate the variable, we calculate the value total_walks, which is our variable by multiplying total_dogs with the total_days.

These are the two parameters that have been passed to the subroutine.

The return value for this subroutine will be this total walks that have been calculated.

So the statement becomes return total_walks.

Let's do a quick recap before we move on.

What is the correct syntax to define a subroutine in Python? Why don't you pause the video here and have a think? Did you choose option A? That's right.

Well done.

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 number of dogs, multiplied by number of days.

Calculate the total cost based on number of walks multiplied by four, where four is the cost per walk.

And finally, display the relevant invoice information: number of dogs, number of days, total number of walks, and total cost.

Let's just do a quick recap.

Based on the plan for the dog walking system, which subroutine do you think would it be best to create first? Why don't you pause the video here and have a think? You got that one right.

It is option C, number of dogs.

Both the invoice and total number of walks subroutines require return values from other subroutines.

So it is best not to create these first.

Well done on reaching the final task of this lesson.

This will help you make sure that you understood what we covered in this section.

Open the starter programme by clicking on this link.

Using the interface plan from Task A and the following requirements, complete the programme.

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 number of dogs multiplied by number of days, calculate the total cost based on number of walks multiplied by four, where four is the cost per walk.

And finally, display the relevant invoice information, which is number of dogs, number of days, total number of walks, and the total cost.

Why don't you pause the video here and see if you can work this programme out? Right, so this is what the final Python programme looks like.

Did you work that out? I'm sure you did.

So we have five subroutines, which are called from the main body of the programme.

So the five subroutines are num_dogs, num_days, num_walks, total charge and invoice.

Num_dogs asks the user to enter the number of dogs and then returns the value to the main body of the programme.

Num_days asks the user to enter the number of days and then returns the total number of days back to the main programme.

These values are stored in total_dogs and total_days variables respectively.

The num_walks sub routine takes the parameters total_dogs and total_days, and then calculates the total number of walks.

The value returned, the total number of walks, is stored in the variable total_walks.

The total_charge subroutine takes the parameter total_walks as an input and then calculates the total cost.

The return value for this subroutine is total_cost, which is stored in the variable total_cost.

And then finally, the invoice subroutine takes the parameters total_dogs, total_days, total_walks, and total_cost, and then prints these values out on the screen.

You can see the text output.

The user is asked to enter the number of dogs.

The user enters 2.

Enter the number of days, the user enters 10, and then the programme returns the values the number of dogs walked and the number of days walked, calculates the total number of walks by multiplying the days walked and the dogs walked.

So the answer would be 20.

The total cost of the invoice is 4 multiplied by 20, which is 80.

So you can see that the text output on the screen will look like this when you run your programme.

Well done for getting this far.

We have come to the end of today's lesson, and you have done brilliantly.

To summarise: In structured programming, a programme can be divided into a set of smaller sections called subroutines.

Each subroutine 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 subroutines.