One Function, Different Parameters

Because Clojure—and Clojure programmers—do rely so heavily on functions, it’s not surprising that the language provides some extras to help you craft just the function that you want. For example, all the functions we’ve written so far have taken a fixed number of arguments. Sometimes it’s convenient to build functions that take a less doctrinaire view of how many arguments they are willing to accept. For example, we might want to create a Hello, World–style function that will let you supply a greeting or default to a plain old “Hello”. This is not hard:

 (​defn​ greet
  ([to-whom] (println ​"Welcome to Blotts Books"​ to-whom))
  ([message to-whom] (println message to-whom)))

The greet function ...

Get Getting Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.