Dylan methods declare parameters in fashion similar to that of conventional languages, except for the fact that parameters may optionally be untyped. Both of the following methods are legal:
define method foo(x :: <integer>, y) end; define method bar(m, s :: <string>) end;
Both foo and bar have one typed and one untyped parameter, but neither has a well-defined return value (or actually does anything). As in C, each typed parameter must have its own type declaration; there's no syntax for saying " the last three parameters were all integers".
Functions with variable numbers of parameters include the #rest keyword at the end of their parameter lists. Thus, the declaration for C's printf function would appear something like the following in Dylan:
define method printf(format-string :: <string>, #rest arguments) => (); // Print the format string, extracting // one at a time from "arguments". Note // that Dylan actually allows us to // verify the types of variables, // preventing those nasty printf errors, // such as using %d instead of %ld. // ... end method printf;
For an actual implementation of a lightweight printf function, see Appendix A.
Note that Dylan makes no provision for passing variables by reference in the Pascal sense, or for passing pointers to variables. Parameter names are simply bound to whatever values are passed, and may be rebound like regular variables. This means that there's no way to write a swap function in Dylan (except by using macros). However, the following function works just fine, because it modifies the internal state of another object:
define method sell(car :: <car>, new-owner :: <string>) => (); if (credit-check(new-owner)) car.owner = new-owner; else error("Bad credit!"); end; end;
If this sounds unclear, reread the chapter on variables and expressions.