[Robot]
  Reeborg the Robot  

Functions



Extending Reeborg's Vocabulary

Reeborg's vocabulary is initially very limited, consisting of only five built-in instructions. We would get cramps writing a program that has Reeborg move, say, 10 miles (1 mile = 8 blocks); we would have to type 80 move()'s! Also, programming some actions using only the built-in instructions can be awkward. For example, to have to write three turn_left()'s for Reeborg to turn right seems unnatural. Happily a way exists to extend Reeborg's vocabulary: user-defined functions. The word function is just another term for the type of instructions that Reeborg executes.

To teach Reeborg new functions (instructions) we define these new functions in terms of functions that Reeborg already knows or has learned. In a sense, user-defined functions allow us to create a dictionary consisting of new words that Reeborg can recognize. Like in a dictionary, these new words are accompanied by a definition of the new function. To illustrate how this is done, let us see what a user-defined turn_right() function definition looks like:

    def turn_right():
        turn_left()
        turn_left()
        turn_left()
If we insert this definition in a Reeborg program, we can use the turn_right() instruction in directing Reeborg what to do. In other words, we have now taught Reeborg to turn right by executing three left turns. Note that turn_right() for Reeborg means exactly what the instructions indented after the def heading line say. The fact that turn_right has somewhat of a meaning in the English language is of no concern to Reeborg. We could have defined:
    def self_destruct():
        turn_left()
        turn_left()
        turn_left()
If Reeborg was asked to execute the self_destruct() instruction he would of course not explode or destroy himself, but rather would make three left turns.

To indent instructions in a Python program, space characters (i.e., blanks) should be used, not tabs. In Python, 4 blanks are usually used to indent statements.

IDENTIFIERS: The name of a function is called an identifier. An identifier consists of the following characters: A-Z, a-z, 0-9, and the underscore. The identifier cannot contain any blanks and must begin with an alphabetic letter. (Actually, it can begin with an underscore, but there are circumstances where this can cause problems so it is best not to do so.)


The Stair Cleaning Program

To illustrate how to use user-defined functions and where they are placed in a Reeborg program, consider the following program example. Note that execution starts with the first code at the botton of a Python program that is not part of a function definition. As a consequence, it is fairly typical (but not required) to define a function main() that contains the code that we wish to start executing first. Then, at the bottom of the program, there is a statement that invokes this main() function. Despite this strange ordering, Reeborg never gets confused when running a program.
The Stair Cleaning Task

Reeborg is supposed to climb stairs and pick up the beepers on each step.
When he is done, he should be standing on the top step, facing East.



# A Stair Cleaning Program
def main():
    climb_a_step()
    pick_beeper()
    climb_a_step()
    pick_beeper()
    climb_a_step()
    pick_beeper()
    turn_off()

# Climb on to next step
def climb_a_step():
    turn_left()
    move()
    turn_right()
    move()

# Pivot Reeborg 90 degrees to right
def turn_right():
    turn_left()
    turn_left()
    turn_left()

# Invoke main()
main()

Good Programming Style

A program that works perfectly is not considered a good program; it is simply a working program. To write a good robot program you must follow these guidelines:
    Example:

During the summer Reeborg works as a field laborer. His task is to harvest the field of beepers. He can relax and turn off as soon as he has harvested all beepers and stepped out of the field.

Here are two sketched solutions to the problem. The one on the right makes use of defining new functions which are supposed to do exactly what their English equivalent indicates. Which of these programs would be considered the better program? (Click on the critique link below each program to check your answer.)

Program 1 Program 2
        move()
        pick_beeper() 
        move()
        pick_beeper()
        ...
        turn_left()
        move()
        turn_left()
        pick_beeper()
        move()
        ...
        turn_left()
        turn_left()
        turn_left()
        move()
        turn_left()
        turn_left()
        turn_left()
        pick_beeper()
        move()
        ...
        turn_off()
        move()
        harvest_2_rows()
        position_for_next_harvest()   
        harvest_2_rows()
        position_for_next_harvest()   
        harvest_2_rows()
        move()
        turn_off()
Program 1 Critique Program 2 Critique



Prior Topic       |       Next Topic  

[MTSU]  | CS  | [Home] Return to Reeborg home page
(Credits and Copyrights)