Procedures
Programming with Logo - Lesson 5
Objectives
- To create and understand a Logo procedure
- To combine procedures together
- To use repeat commands in Logo to draw regular shapes
Lesson Resources
Lesson 1 - Introducing Logo
Lesson 2 - Drawing Words
Lesson 3 - Adding Colour
Lesson 4 - Repeat Commands
Lesson 5 - Procedures
Lesson 6 - Progressing To Scratch
Introduction
Recap the last few lessons and the new commands that we have learned. How did we change the colour of the pen? (setpc n) How did we do a quarter turn? (lt/rt 90) How did we do a diagonal turn to make more of a curved line? (lt/rt 45) How did we repeat an instruction to draw something like a square? rpt 4 [fd 6 rt 90]
What is a procedure?
We know that computers can only understand certain commands and that they need really precise instructions, so we must be careful when we program them. A repeat command last week saved us some time, but with Logo we can save even more time by teaching the computer new commands! These are called procedures.
First we need to give our procedure a name, then tell the computer what we want it to do when we use that name as a command and then the computer will remember it. After that we can just use the new procedure name to make the computer perform the commands we saved.
Here’s an example.
Click on the + in the My Procedures box.
A Procedure box will then pop up.
In the 'Name' box we'll type 'square'. Below that, we'll add the commands for how to draw a square. Let’s use the repeat command we learned last week:
You can even test it out before you save it by clicking the green play button.
Then click the OK button to save this procedure. Now in the command line type ‘square’ and your Logo robot will draw a square (or perform whatever commands you assigned to the word ‘square’).
Ask the students to have a go at this to make their own first procedure.
A triangle procedure
Let’s have another go to make a ‘triangle’ procedure. What do I do first to make a new procedure? Type ‘to triangle’ or you can shorten this to just ‘to tri’ if you prefer.
What do we add to this next box? How many repeats will it need for a triangle? What goes inside the brackets? Ensure the command reads:
rpt 3 [fd 10 lt 120]
Press OK to save and then test out the command triangle (or tri if you used that).
Combining Procedures
How could we combine our two procedures to draw a house? A square with a rectangle on top of it would do this nicely! Let’s try it out, type in ‘triangle square’ and see what it draws. It’s likely you will have a wonky roof the first time you try this. What went wrong? Can you help me debug what happened? Do we need any extra code to make this work?
Clear the screen and return the robot to the centre (home) to try again. The key to this working well is having the robot facing to the right before we start as we want the bottom of the triangle to be straight. So type the command ‘rt 90 triangle square’ and you should get something like this:
We can simplify this even further by creating a ‘house’ procedure! Make a new procedure called 'house’. What commands should we add to the box this time? We don’t need to repeat any of the longer commands for drawing a square and a rectangle again as we’ve taught the computer new commands that we can also use here. Just type ‘triangle square’ and then try out the command ‘house’
You can make a row of houses very quickly by using a repeat command and adding fd 7 (or whatever number you used for the sides of your square). Also, make sure your turtle is facing to the right when you start.
rpt 4 [house fd 7]
And don't forget you can experiment with the pen and colour settings we learned in the last lesson.
Procedure patterns
You can do some other pretty cool things with procedures and repeat commands. Ask the students to try this one out:
rpt 10 [square rt 36]
How does it work? It draws 10 (rpt 10) squares (square procedure) and turns 36 degrees after it has completed each square. Can you see the first few squares? Trace these with your finger or cursor for the children to see.
Before you continue, it’s worth putting Logo in fast mode or you will lose a lot of time waiting for the shapes to be drawn, just use the speed slider in the top right of the screen.
Now try this one:
rpt 36 [square rt 10]
What’s different about this one? It does more repeats and uses a smaller angle of turn. What is similar? It still draws squares.
Main task
The children can experiment with creating their own patterns if they follow these simple steps
- You have to make a procedure for a shape before you can use it in a pattern
- Add the name of your shape procedures to one of these commands:
-
- rpt 10 [name of procedure rt 36]
- rpt 36 [name of procedure rt 10]
- rpt 60 [name of procedure rt 60]
- Test it out! Don’t forget you can change the colours of your patterns using the setpc commands from previous lessons.
Here’s some more examples:
Remember, these won’t work until you create the procedures for the shapes first!
rpt 6 [hexagon rt 60]
rpt 36 [circle rt 10]
rpt 10 [triangle rt 36]
rpt 10 [pentagon rt 36]
Repeat commands for different shapes to use:
square - rpt 4 [fd 7 rt 90]
triangle - rpt 3 [fd 7 rt 120]
pentagon - rpt 5 [fd 5 rt 72]
hexagon - rpt 6 [fd 4 rt 60]
decagon - rpt 10 [fd 3 rt 36]
circle - rpt 30 [fd 1 rt 12]
Plenary
What is a procedure? A way to teach the computer a new command. Why would we want to use procedures? Once they are created, they let us code much faster, especially if it’s for a set of commands that we might use a lot.
If we had to make a procedure for leaving the ICT suite, what individual commands would it contain? Save work, Log off, tidy keyboard and mouse, stand up, tuck chair under, walk to the line, wait quietly. Let’s try out this procedure now we have learnt it!
Point out that we do this as humans all the time, but it is learnt behaviour and can’t be followed accurately the first time. ‘Get ready for school’, ‘tidy up’, ‘make the breakfast’ are all procedures that contain many smaller steps if we thought about the whole algorithm for doing them, but over time we can learn and remember that the procedure means do all those things.