Base Concepts of Functions
Explore the foundational concepts of functions in Clojure, including how to declare and define functions and variables. Understand Clojure's syntax with parentheses, the def and defn keywords, and get introduced to core libraries like clojure.pprint, clojure.math, and clojure.test essential for effective functional programming.
Syntax and semantics
Clojure uses the Lisp dialect, as mentioned in previous lessons, and one of its main characteristics is the different style of its syntax and semantics, which is full of parentheses.
Just to remember the base idea of how we read and plan it, let's look at the image below (also seen previously).
So, the whole code will look just like this: a parenthesis being the invoker, a function-name, which is the action being invoked, and its parameters.
With this explanation, we know exactly how to invoke and call functions, right? However, how do we create functions of our own?
Declaring variables and functions
In Clojure, as in any other programming language, we have a reserved word to define that we are creating a function of our own—defn—and a reserved word to define a global variable—def.
Note: Both
defanddefnare actually functions. ...