3.1 Method definitions

In Dylan, we define methods — a method is a kind of function. We define a simple method, say-hello, as follows:

define method say-hello ()
  format-out("hello, world\n");
end;

We call say-hello as follows:

? say-hello();
hello, world

We use define method to define a method named say-hello. Just after the name say-hello, we specify the method's parameter list, (). The parameter list of this method is empty, meaning that this method takes no arguments. The call to say-hello provides an empty argument list, meaning that there are no arguments in the call.

The body of the say-hello method has one expression — a call to format-out. A method returns whatever is returned by the expression executed last in its body. In general, a method can return a single value, multiple values, or no value at all. The say-hello method returns what format-out returns — no value at all. In the call to say-hello, we see the output of format-out in the listener; we see output and not a returned value (because no value is returned).

Usage note: In this chapter, we define methods that call the format-out function. Because format-out is in the format-out module, we need to make that module available. There are two ways to do so. The first way is to work in files, as described in Section 2.5. The second way is to use a gesture or command in your Dylan environment to make the format-out module accessible. Then, you can simply enter the method definitions into the listener.