Loading...
Hi, my name's Mr. Hall, and thank you for joining me in this lesson today.
This lesson is called Procedures in programming, taken from the unit Repetition in programmes.
I'm really looking forward to learning with you today, so let's get started.
Today's learning outcome is that you'll be able to decompose a task into small steps.
We've got two keywords in this lesson.
The first one is decompose, which you've already seen in the learning outcome.
So decompose means to break down a task into smaller, more achievable steps.
And the second keyword is procedure.
A procedure is a named set of commands that can be called multiple times throughout a programme.
So there's our keywords, decompose and procedure.
We've got two learning cycles in this lesson.
First, you're going to learn how to identify chunks of actions in programming tasks, and then we'll move on to using procedures in a programme to simplify code.
So let's get started with that first learning outcome.
Decomposing a task means breaking it down into smaller, more manageable steps.
This is a skill used every day.
Many tasks are made up of smaller steps that work together to complete the whole task.
Which everyday tasks can you think of that have many smaller parts? Here's some examples of some tasks that can be decomposed.
Aisha suggested getting ready for school.
Lucas has added making a sandwich.
And Jacob has said washing your hands.
These are all great examples of tasks that can be decomposed.
Let's now have a look at another task which you can decompose.
The task of getting ready for PE has lots of different parts, like going to get your PE kit, taking off your shoes, and many more.
Can you decompose the action of getting ready for PE by breaking it down into smaller parts? So here's how you could decompose that process.
So the first bit you could break down in smaller parts is to collect your kit.
And in doing that, you'll probably need to walk to the cloakroom, then take your PE bag off the hook, and then carry your PE bag back to the classroom.
Once you've done that, you need to get undressed.
So to do that, first of all, you'll put your kit on the chair, take off your uniform, and then put your uniform on the chair.
Then you need to put on your PE kit.
You could put on your top, put on your shorts, and put on your trainers.
Then once you've done all that, you can go and sit on the floor.
Decomposition is an important skill used in programming.
Logo is a text-based programming language.
You type Logo commands that make the turtle draw on the screen.
These are some useful Logo commands.
fd for forward.
bk for backward.
rt for right turn.
lt for left turn.
cs for clear screen.
pu for pen up.
pd for pen down.
And repeat to repeat a section of code.
This is how the repeat command in a count-controlled loop in Logo might look.
So first of all, you have your command, which is repeat, then a space, then how many times do you want to repeat, so this time we want to repeat four times, then a space, and then inside the square brackets what we want to repeat.
So this example has forward, [fd 100 rt 90].
So the whole line of code is repeat 4 [fd 100 rt 90].
Now rearrange these commands to make the code for drawing a hexagon.
Well done.
This is what your code should look like.
So you should have repeat 6, so six sides for a hexagon, space [fd 100 rt 60], and 60 is the angle of turn you need to draw a hexagon.
When we think about what we need to programme, we can also decompose our task into smaller parts.
For example, when planning how to draw a hexagon, we can break it down into these parts.
So first, you'll want to clear the screen.
Then, you'll want to go forward 100 steps.
Then, turn right by 60 degrees.
And you want to repeat steps two and three six times so you get all six sides of your hexagon.
To help you do that you can use procedures.
A procedure is a named chunk of code that can be run multiple times.
Creating a procedure helps you save time later on, as you can run the whole chunk of code by typing the one word that you used as the procedure name.
The way that Logo creates procedures can look different in different environments, such as a new menu, which you can see in this image.
Check with an adult about which type you're using and how this feature looks, as it may look a bit different to the example you can see on the screen.
Watch this animation demonstrating creating a procedure.
So first of all, our procedure is gonna be to draw a square, so we type to square.
And then in this box, we type what we want the procedure to be.
So it's repeat 4 [fd 100 rt 90], click end, and then we type square in the instruction box, and that will draw a square.
So I'll let that run through again so you can see it for a second time.
So, to square, and then what we want that procedure to be.
Once you've created that procedure by typing square, the programme will draw a square for you.
So here's how you create a procedure for a square.
So you would type to square with a space, and then repeat 4 [fd 100 rt 90], and then end.
And that's your procedure for a square.
So square is the name of the procedure, and there is your code to draw a square.
To run a procedure you just need to type its name.
Now Alex has a really useful tip here.
"This won't do anything if you haven't created the procedure first!" So you must create the procedure before you can type its name.
A procedure can save you time when you've got a longer programme, as you only need to type the name of the procedure when you need it.
This action is called calling a procedure.
So there's our procedure, and that's the code to draw a square.
And as Lucas points out, square is a lot easier to type out than that whole line of code.
So to create a procedure for drawing a triangle, you would say to triangle, and then the code for drawing a triangle, which is repeat 3 [fd 100 rt 120], and then end.
So there's the name of our procedure, triangle, and the code to draw a triangle.
Choose a name for your procedure that matches what it does.
This makes your code easier for you to read and understand.
The name of the procedure that you choose has no meaning at all for the computer.
The computer runs the commands that you give it.
So naming it properly is just to help you.
So here we have an example, triangle being the procedure.
Fill in the blanks in these sentences.
A blank is a named chunk of code that can be blank multiple times.
Creating a procedure will save blank because once you've made it, you can run it just by typing the blank.
Well done.
A procedure is a named chunk of code that can be called multiple times.
Creating a procedure will save time because once you've made it, you can run it just by typing the name.
So your task is to create a procedure for a hexagon, give it an appropriate name, and then test your procedure in Logo to check that it works.
So this is how you create and test a procedure for a hexagon.
So to create the procedure, you would type to hexagon, and then the instructions for all the code for creating a hexagon, which is repeat 6 [fd 100 rt 60] and end.
Your second task.
A decagon has 10 sides.
Create a procedure for a decagon.
Give it an appropriate name, and then test your procedure in Logo to check that it works.
So here's how you create a procedure for a decagon.
So, to decagon and then repeat 10 [fd 100 rt 36], and then end.
Let's move on to the second learning cycle, which is using procedures in a programme to simplify code.
Procedures, along with count-controlled loops, can be used to make a programme more simple to write.
You're going to use procedures to make a pattern.
Look at these examples of patterns drawn in Logo.
So we've got three examples of patterns.
The first one is made up of a triangle, which is repeated 10 times.
So it says repeat 10, and then inside the square brackets we've got triangle, which is the procedure, and then a right turn of 36 degrees.
So the bit that's being repeated there is the procedure triangle, followed by a right turn of 36.
Then the same kind of structure for the second example.
This time we're repeating 18 times, drawing a hexagon followed by a right turn of 20 degrees.
And finally, the last one is a square, which is repeated eight times with a right turn of 45 degrees.
So there's three examples of patterns that you can draw in Logo using repeats and procedures.
So these patterns were made first by creating a procedure for a shape.
Then the procedure was used in a programme that repeats the shape, turning the turtle to draw the shape at different angles.
And you can see in the images there are the steps along the way.
So first of all you start with a single hexagon, and then if you repeat it a set number of times, you get multiple hexagons with a turn in between each one.
And then eventually, once you get the right number of repeats, you can get the whole shape to complete.
Time for a true or false question.
The name of a procedure changes how it works.
Is that true or false? Well done, it's false.
Can you explain why, though? Well done.
The name of the procedure has no meaning to a computer.
It's just a new command you've created that you can use in your programme.
To use procedures in a programme, you'll need to complete a plan.
This will help you imagine how you want your pattern to look and decide on the code you need to make your programme work.
So first of all, in the first box, you need to name the procedure that you want to use.
So we're gonna use a square in this pattern.
And you'll name your procedure after the shape.
Then we need to put the code for the procedure in.
So this will be to square, and then repeat 4 [fd 100 rt 90], and then end.
So there's the code for the procedure.
Then we need the code for the pattern, which is repeat 10 [square rt 36] In the final box, you'll draw what you think your pattern will look like.
You can trace your code to check you've included all the parts that you wanted to.
Time for a question.
What is not a good reason to use a plan? Is it A, it helps you imagine how you want your final result to look? B, it helps you decide on the code you need to make your programme work? Or is it C, it helps your programme run faster? Well done, the answer is C.
A plan will not help your programme run faster, but it will help you imagine how you want your final result to look, and it will help you decide on the code you need to make your programme work.
So now you can fill in your plan.
Make sure you include the named procedure that you will use, the code for your procedure, the code for your pattern, and your prediction of the pattern.
Here's an example.
So the procedure that I will use is square.
The code for the procedure is to square, and then repeat 4 [fd 100 rt 90].
The code for the pattern is repeat 10 [square rt 36].
And this is what I predict will be created.
So the shape at the bottom.
Use your plan to create your programme in Logo.
If your programme doesn't work as you expect, debug it to find and fix any errors.
If your programme does work, explain how you could improve it or add to your programme in the future.
Let's have a look at this animation to see the pattern programme.
So the code is repeat 10, then calling the procedure square, followed by a right turn of 36 degrees, and there we have the pattern.
And if we clear screen, we can call a different procedure.
So this time, repeat 36, and the procedure square with a right turn of 10, and that creates a different pattern.
So let's just let that run through again.
So the first one is repeated 10 times, drawing a square with a turn of 36 degrees in between each time creates that pattern.
And then if we repeat it instead 36 times, again calling a square, but this time with a turn of 10 degrees, we get that pattern.
Here's a summary of today's lesson.
It's helpful to decompose a task by breaking it down into smaller steps.
A procedure is a set of commands that have been given a name so it can be used multiple times.
And finally, the procedure should be named appropriately.
Using procedures makes your code easier to reuse and change.
Thank you for taking part in today's lesson.
I've really enjoyed learning with you, and I look forward to seeing you again soon.