Karel Can't Turn Right
You may have noticed that Karel can not turn right. Karel can only turn left. To get Karel to turn right, you have to write out three consecutive turnLeft();
commands like so:
Wouldn't it be easier if we had a turnRight();
command that did the same thing?
Methods
In the previous chapter, we introduced the run method. This method is where our program starts executing our commands.
We can create additional methods using a similar syntax. A method is how we teach Karel new commands. By using the power of methods, we can teach Karel how to turnRight();
. Here is the turnRight()
method:
We write all of our commands within the method body; this is the area between the opening and closing curly brackets ({}
).
Tower and Turn Right
Let's have Karel build a tower and then turnRight();
using methods.
While this solution works, we aren't using a turnRight()
method. Let's fix that.
We replaced our three turnLeft();
commands with one turnRight();
command, but the program still does not work. Why is that?
When we run the program, we get an error message telling us that Karel does not know how to turn right. We still need to teach Karel how to turnRight();
by writing a turnRight()
method.
By writing a turnRight()
method, we have taught Karel a new command and completed the program.
Last updated