Skip to main content
TopMiniSite

Back to all posts

How Do Functions Work In Elixir?

Published on
3 min read
How Do Functions Work In Elixir? image

Best Functional Programming Books to Buy in October 2025

1 Elixir in Action, Third Edition

Elixir in Action, Third Edition

BUY & SAVE
$47.40 $59.99
Save 21%
Elixir in Action, Third Edition
2 Programming Elixir ≥ 1.6: Functional |> Concurrent |> Pragmatic |> Fun

Programming Elixir ≥ 1.6: Functional |> Concurrent |> Pragmatic |> Fun

BUY & SAVE
$32.87 $47.95
Save 31%
Programming Elixir ≥ 1.6: Functional |> Concurrent |> Pragmatic |> Fun
3 Elixir Patterns: The essential BEAM handbook for the busy developer

Elixir Patterns: The essential BEAM handbook for the busy developer

BUY & SAVE
$49.00
Elixir Patterns: The essential BEAM handbook for the busy developer
4 Elixir Programming Mastery: An In-Depth Exploration for Developers

Elixir Programming Mastery: An In-Depth Exploration for Developers

BUY & SAVE
$29.99
Elixir Programming Mastery: An In-Depth Exploration for Developers
5 The Art of Elixir: elegant, functional programming

The Art of Elixir: elegant, functional programming

BUY & SAVE
$39.99
The Art of Elixir: elegant, functional programming
6 Engineering Elixir Applications: Navigate Each Stage of Software Delivery with Confidence

Engineering Elixir Applications: Navigate Each Stage of Software Delivery with Confidence

BUY & SAVE
$60.71
Engineering Elixir Applications: Navigate Each Stage of Software Delivery with Confidence
7 Designing Elixir Systems With OTP: Write Highly Scalable, Self-healing Software with Layers

Designing Elixir Systems With OTP: Write Highly Scalable, Self-healing Software with Layers

BUY & SAVE
$41.00
Designing Elixir Systems With OTP: Write Highly Scalable, Self-healing Software with Layers
8 Introducing Elixir: Getting Started in Functional Programming

Introducing Elixir: Getting Started in Functional Programming

BUY & SAVE
$18.49 $24.99
Save 26%
Introducing Elixir: Getting Started in Functional Programming
+
ONE MORE?

In Elixir, functions are defined using the def keyword followed by the function name and parameters. Functions can have multiple clauses, which allows for pattern matching based on different inputs. When calling a function, Elixir will try each clause sequentially until it finds a match.

Functions can also be defined anonymously using the fn keyword, which is commonly used in higher-order functions and passing functions as arguments to other functions. The do keyword can be used to define a multi-line anonymous function.

Elixir also supports recursion, where a function calls itself within its own definition. Recursion is commonly used in Elixir instead of traditional loop constructs like for or while.

Functions in Elixir are first-class citizens, meaning they can be passed around as arguments, assigned to variables, and returned from other functions. This makes Elixir a functional programming language, where functions play a central role in the program's logic.

How to compose functions in elixir?

In Elixir, functions can be composed using the |> operator which is known as the pipe operator. This operator allows us to pass the result of one expression as an argument to the next expression.

Here's an example to illustrate function composition in Elixir:

# Define two functions double = fn x -> x * 2 end square = fn x -> x * x end

Compose the functions

result = 2 |> double |> square

IO.puts result

Output: 16

In the above example, the double function is applied first to the value 2 and then the square function is applied to the result of the double function. The final result is 16.

You can compose multiple functions in this way to create complex operations. Keep in mind that the functions should be compatible in terms of their input and output types for successful function composition in Elixir.

How to call a function in elixir?

To call a function in Elixir, you simply need to write the function name followed by parentheses ().

For example, if you have a function named add that takes two arguments and returns their sum, you would call it like this:

result = add(2, 3)

If the function is defined in a module, you will need to prefix the function name with the module name. For example, if the add function is defined in a module named Math, you would call it like this:

result = Math.add(2, 3)

You can also use anonymous functions in Elixir by using the fn keyword. For example:

add = fn a, b -> a + b end result = add.(2, 3)

In this case, the add function is an anonymous function that takes two arguments and returns their sum. The . after add is the Elixir syntax for calling an anonymous function.

What is function arity in elixir?

In Elixir, function arity refers to the number of arguments that a function can accept. Each function in Elixir has a fixed arity, meaning that it can only accept a specific number of arguments. This is different from languages like JavaScript or Ruby, where functions can accept a variable number of arguments.

For example, a function with an arity of 2 can accept exactly 2 arguments, and if called with either too few or too many arguments, it will result in a compilation error.

It is important to be aware of the arity of functions when calling them in Elixir, as providing the wrong number of arguments can lead to unexpected results or errors.