Functions and Variables - oliyh/learning-clojure GitHub Wiki

Assign a value to a var using def

(def my-name "Darth Vader")
my-name ;; Darth Vader

Functions are first-class members of Clojure, as you'd expect from a functional language. You can reference them, pass them around, chain them up!

hello is a function that takes no arguments:

(def hello (fn [] (println "Hello World")))

To execute the function, wrap it in parentheses

(hello) ;; Hello World

The shortcut

(defn hi [] (println "Hi World"))
(hi) ;; Hi World

Arguments

Arguments are expressed in the function's bindings, as follows:

(defn greeting [your-name] (println "Greetings," your-name))
(greeting "Yoda") ;; "Greetings, Yoda"

Arguments can be functions!

(defn do-maths [f a b] (println "Input:" [a b] "Output:" (f a b)))
(do-maths * 5 6) ;; Input: [5 6] Output: 30