
- Clojure Tutorial
- Clojure - Home
- Clojure - Overview
- Clojure - Environment
- Clojure - Basic Syntax
- Clojure - REPL
- Clojure - Data Types
- Clojure - Variables
- Clojure - Operators
- Clojure - Loops
- Clojure - Decision Making
- Clojure - Functions
- Clojure - Numbers
- Clojure - Recursion
- Clojure - File I/O
- Clojure - Strings
- Clojure - Lists
- Clojure - Sets
- Clojure - Vectors
- Clojure - Maps
- Clojure - Namespaces
- Clojure - Exception Handling
- Clojure - Sequences
- Clojure - Regular Expressions
- Clojure - Predicates
- Clojure - Destructuring
- Clojure - Date & Time
- Clojure - Atoms
- Clojure - Metadata
- Clojure - StructMaps
- Clojure - Agents
- Clojure - Watchers
- Clojure - Macros
- Clojure - Reference Values
- Clojure - Databases
- Clojure - Java Interface
- Clojure - Concurrent Programming
- Clojure - Applications
- Clojure - Automated Testing
- Clojure - Libraries
- Clojure Useful Resources
- Clojure - Quick Guide
- Clojure - Useful Resources
- Clojure - Discussion
Clojure - Defining a Function
A function is defined by using the defn macro. Following is the general syntax of the definition of a function.
Syntax
(defn functionname optional documentation string [arguments] (code block))
Functions can have documentation strings, which is good to describe what the function actually does.
Example
Following is a simple example of a function.
(ns clojure.examples.hello (:gen-class)) ;; This program displays Hello World (defn Example [] (def x 1) (def y 1.25) (def str1 "Hello") (println x) (println y) (println str1)) (Example)
In the above example, the name of the function is Example.
Output
1 1.25 Hello
clojure_functions.htm
Advertisements