Lesson 13. functions

 

Clojure belongs to the family of functional programming languages. As so, the basic building blocks of the language are functions.

In Clojure, functions are first-class citizens. It means that in addition of being able to create functions in some global scope, we can manipulate them in the language like other objects. For example,

  1. We can create a function in the middle of another function
  2. We can create variables whose values are functions
  3. We can have collections whose members are functions
  4. We can pass a function as an argument to another function
  5. We can return a function from another function

We will investigate deeply each of the above properties of functions throughout the book. In this Lesson, we will focus on learning the syntax for creating functions in a global scope and discuss interesting properties of functions like arity and purity.

Much more details regarding the definition of a function (e.g. default values, docstrings, mutliarities, variadic) will be covered in Unit 13. In this Lesson, we will focus on the basics.

After completing this lesson, you will be able to:

  • write custom functions with or without parameters
  • write custom functions that call custom functions that call custom functions...
  • display the arity of a function in the REPL
  • determine whether a function is pure or impure
  • explain why in Clojure, arguments passing by reference or by value is never a concern

13.1   Function definition syntax

13.2   Function arity

13.3   Pure and impure functions

13.4   Variable number of arguments

13.5   Functions return a single value

13.6   By reference or by value?

13.7   Summary