A Function of Your Own

Let’s return to our Hello, World example and see if we can turn our one-liner into something more worthy of the name program. We can do this by wrapping it with the fundamental unit of Clojure code, the function:

 (​defn​ hello-world [] (println ​"Hello, world!"​))

Once you have hello-world defined you can call it just like any other Clojure function, so that if you run

 (hello-world)

You will see Hello, world! printed.

As with the original, you can learn a lot from this new version of Hello, World. For example, you can see that the function definition kicks off with defn instead of def and is wrapped in its own set of parentheses on the outside. Inside the defn we have the function parameters, set off with square brackets, ...

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.