Learn more

Chapter 3: Defining New Methods

In the last chapter we wrote a program to help Karel climb a simple ledge:

Example: FirstKarel

Even though the FirstKarel program above demonstrates that it is possible to perform the turnRight operation using only Karel’s built-in commands, the resulting program is not particularly clear conceptually. In your mental design of the program, Karel turns right when it reaches the top of the ledge. The fact that you have to use three turnLeft commands to do so is annoying. It would be much simpler if you could simply say turnRight() and have Karel understand this command. The resulting program would not only be shorter and easier to write, but also significantly easier to read.

Defining New Commands

Fortunately, the Karel programming language makes it possible to define new commands simply by including new method definitions. Whenever you have a sequence of Karel commands that performs some useful task--such as turning right--you can define a new method that executes that sequence of commands. The format for defining a new Karel method has much the same as the definition of run in the preceding examples, which is a method definition in its own right. A typical method definition looks like this:

   private void name() {
commands that make up the body of the method

In this pattern, name represents the name you have chosen for the new method. To complete the definition, all you have to do is provide the sequence of commands in the lines between the curly braces. For example, you can define turnRight as follows:

   private void turnRight() {

Similarly, you could define a new turnAround method like this:

   private void turnAround() {

You can use the name of a new method just like any of Karel’s built-in commands. For example, once you have defined turnRight, you could replace the three turnLeft commands in the StepUpKarel program with a single call to the turnRight method. Here is a revised implementation of the program that uses turnRight:

There is, of course, one obvious difference between the definitions of the run and turnRight methods in the program above: the run method is marked as public in contrast to turnRight, which is marked as private. The difference between these two designations is that public methods can be invoked from outside the program, while private methods cannot. The run method needs to be public because the Karel environment needs to be able to issue a run command to get things going. It is generally good programming practice to keep definitions private whenever possible.

Method Code Blocks

A group of commands between two curly brackets { } is called a code block. The body of your program is a code block as is the body of a method. Notice how the contents of a code block are all indented one tab in. This is important stylistically since it makes it easy for humans to know what lines of code are in a given block.

You can define as many methods as you want. They should all be written one after another inside your program code block. You can't define a method inside another method.

Next Chapter