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:

   def 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 after the colon, which are all indented by one tab. For example, you can define turnRight as follows:

   def turnRight():

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

   def 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 follow the colon : which are indented, is called a code block. The body of your method is a code block. Notice how the contents of a code block are all indented one tab in. This is important functionally since it allows Python 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. You can't define a method inside another method.

Next Chapter