Product Site

1.5. Methods

Sending a message to an object results in performing some action; that is, it executes some underlying code. The action-generating code is called a method. When you send a message to an object, the message is the name of the target method. Method names are character strings like reverse. In the preceding example, sending the reverse message to the !iH object causes it to run the reverse method. Most objects are capable of more than one action, and so have a number of available methods.

The classes Rexx provides include their own predefined methods. The Message class, for example, has completed, init, notify, result, send, and start methods. When you create your own classes, you can write new methods for them in Rexx code. Much of the object programming in Rexx is writing the code for the methods you create.

Rexx lets you send the same message to objects that are different:
Example 1.1. Methods
"!iH"~reverse   -- Reverses the characters "!iH" to form "Hi!"
pen~reverse     -- Reverses the direction of a plotter pen
ball~reverse    -- Reverses the direction of a moving ball

As long as each object has its own reverse method, reverse runs even if the programming implementation is different for each object. Each object knows only its own version of reverse. And even though the objects are different, each reverses itself as dictated by its own code.

Although the !iH object's reverse code is different from the plotter pen's, the method name can be the same because Rexx keeps track of the methods each object owns. You do not need to have several message names like reverse_string, reverse_pen, reverse_ball. This keeps method-naming schemes simple and makes complex programs easy to follow and modify.