Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

clojure
anonymous
communitycreator

What is an anonymous function in Clojure?

Chinweuba Elijah Azubuike

What is a function?

Functions are generally code blocks that carry out certain instructions in an application. These block of code can be called more than once during the runtime or execution time of the application.

What is an anonymous function?

In Clojure an anonymous function is a function without a name and as such, it can not be referenced. Let's see how it works below.

Syntax

(fn  [message]  (println message) )
Syntax of an anonymous function

Let's see an example to understand this clearly.

Example

(ns clojure.examples.hello
   (:gen-class))
   (  (fn [message] (println message))  "John Doe is popular" )
Example of an anonymous function

Explanation

We create an anonymous function that prints a string.

Line 3: Observe the fn syntax used and the argument passed to the function [message]. Since the function is an anonymous function, we only create it anonymous function where it'll be used.

RELATED TAGS

clojure
anonymous
communitycreator

CONTRIBUTOR

Chinweuba Elijah Azubuike
RELATED COURSES

View all Courses

Keep Exploring