2.4 Formatted output

Throughout this book, we use the format-out function to print output. The syntax of format-out is

format-out string arg1 ... argn

The format-out function sends output to the standard output destination, which could be the window where the program was invoked, or a new window associated with the program. The standard output destination depends on the platform.

The string argument can contain ordinary text, formatting instructions beginning with %, and characters beginning with a backslash, \. Ordinary text in the format string is sent to the destination verbatim. You can use the backslash character in the string argument to insert unusual characters, such as \n, which prints the newline character.

? format-out("Your future is filled with wondrous surprises.\n")
Your future is filled with wondrous surprises.

Formatting instructions begin with a percent sign, %. For each %, there is normally a corresponding argument giving an object to output. The character after the % controls how the object is formatted. A wide range of formatting characters is available, but we use only the following formatting characters in this book:

%d

Prints an integer represented as a decimal number

%s

Prints the contents of its string argument unquoted

%=

Prints an implementation-specific representation of the object; you can use %= for any class of object

Here are examples:

? format-out
    ("Your number is %= and mine is %d\n", *your-number*, *my-number*);
Your number is 12.01 and mine is 7.
? format-out("The %s meeting will be held at %d:%d%d.\n", "Staff", 2, 3, 0);
The Staff meeting will be held at 2:30.

In Dylan, functions do not need to return any values. The format-out function returns no values. Thus, it is called only for its side effect (printing output).

Comparison with C: format-out is similar to printf.

The format-out function is available from the format-out library, and is not part of the core Dylan language. We now describe how to make the format-out function accessible to our program, and how to set up the files that constitute the program. Many of the details depend on the implementation of Dylan, so you will need to consult the documentation of your Dylan implementation.

Usage note: The Apple Technology Release does not currently provide the format-out function. For information about how to run these examples in the Apple Technology Release, see Harlequin's or Addison-Wesley's Web page for our book. See Section A.1. in Appendix A, Resources on Dylan.