Functions are generally code blocks that carry out certain execution in an application. This block of code can be called more than once during the execution of the application.
In Clojure, a function is defined using the defn
keyword, followed by the function's name.
(defn nameoffunction“documentation string”[arguments](code block))
Unlike most programming languages that use parenthesis ()
to pass arguments and curly braces {}
for a code block, Clojure uses the square bracket []
to pass arguments and the parenthesis ()
for code block.
Let's look at an example of how to define a function.
(defn func [](println "Hello World"))(func)
func
. The documentation string specified in the syntax is optional, and because we are performing a simple print function, passing a variable was not needed.(func)
function. Note: We did not call the(func)
function as we do in other languages.