HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to ElixirVariablesData TypesOperatorsStringsListsConditions (if / else)Iteration (Enum)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Enum
🔀 Condition
🧩 Function
Elixir›Functions

Functions

Anonymous functions (fn -> end, call f.(x)) and module functions (def).

Simple enough for a 5-year-old to understand

Anonymous function

Defined with fn ... -> ... end; called with a dot: f.(x).

Example code
1
square = fn x -> x * x end
Anonymous function.
FunctionAnonymous function.
2
IO.puts(square.(5))
Called with a dot: 25
Output / InputCalled with a dot: 25
FunctionOutput / Input
💡 Hint: A dot is required when calling an anonymous function: square.(5).

About this topic

Named functions in Elixir are defined inside a module; you cannot define them outside a module. Anonymous functions, on the other hand, can be defined anywhere, but require a dot before being called — this distinction is unique to Elixir.

A function is defined by its name and arity (number of arguments) together. Two functions with the same name but different numbers of arguments are completely separate functions to Elixir. This is why you see numbers next to function names in the documentation.

Pattern matching works in function heads as well. You define the same function with separate heads for different input formats; Elixir chooses the first matching head from top to bottom. It is both shorter than writing conditionals and directly expresses intent.

What you will learn

  • Defining functions inside a module
  • Anonymous functions and their calling syntax
  • The importance of arity
  • Pattern matching in the function head
  • Default arguments

Who is this for?

  • Beginners to Elixir
  • Those getting errors when calling anonymous functions
  • Learners writing multi-headed functions

When to use?

If different behavior is needed based on the input format, writing separate function heads instead of conditionals is the preferred way in Elixir.

Anonymous functions are suitable for short transformations, while named functions are suitable for logic that will be reused.

Common mistakes

Wrong

kare = fn x -> x * x end
IO.puts kare(5)

When calling anonymous functions, a dot is placed between the name and parentheses; this code gives an "undefined function square/1" error. The distinction is intentional: in Elixir, named functions and functions stored in variables are in different namespaces.

Correct

kare = fn x -> x * x end
IO.puts kare.(5)

Wrong

defmodule Hesap do
  def topla(a, b) do
    a + b
  end
end
IO.puts Hesap.topla(5)

A function taking two arguments was called with a single argument; Elixir treats this as a different function and gives an "undefined function" error. Arity is part of the function's identity. If you want flexibility, you must define default arguments.

Correct

defmodule Hesap do
  def topla(a, b \\ 0) do
    a + b
  end
end
IO.puts Hesap.topla(5)

Wrong

defmodule Kontrol do
  def tip(_), do: "other"
  def tip(0), do: "zero"
end

Heads are tried from top to bottom, and since the general head is first, the second is never reached; the compiler warns "this clause cannot match because a previous clause always matches". Special cases should always be written first.

Correct

defmodule Kontrol do
  def tip(0), do: "zero"
  def tip(_), do: "other"
end

Frequently asked questions

Why are anonymous functions called with a dot?

Because in Elixir, function names and variable names are in separate namespaces. The dot specifies that the thing being called is a function stored in a variable; this prevents a function and a variable with the same name from conflicting.

Can guards be used in the function head?

Yes, with the when keyword. It allows you to add conditions that pattern matching isn't enough for; for example, that the argument is positive. Only certain safe functions can be used in guards.

How is a private function defined?

A special version of the def keyword is used; this function can only be called from within its own module. It is used to keep the module's public interface small and hide internal details.

Summary

Named functions are defined inside a module.

Anonymous functions are called with a dot.

Arity is part of the function's identity.

Heads are tried from top to bottom.

Special cases should be written before general cases.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code