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
Anonymous function (fn)def (module function)
Elixir›REFERENCE›Function
🧩

Function

Anonymous (fn) and module (def) functions.

CommandDescription
Anonymous function (fn)Unnamed function.
def (module function)Named function.

Anonymous function (fn)

Try it yourself →

Unnamed function.

Syntax

f = fn param -> expression end;  f.(x)

Description

Defined with fn ... -> ... end and bound to a variable. When called, a DOT is required: f.(x). Frequently used in Enum callbacks.

Example

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

Expected output

25

When to use?

Enum.map/each callbacks, short operations.

Common mistakes

  • ⚠️ A dot is required when calling an anonymous function: square.(5).

def (module function)

Try it yourself →

Named function.

Syntax

defmodule M do  def name(p) do ... end  end

Description

Named functions are defined with def inside a module. Called with Module.function(args) (no dot). The last expression is returned automatically.

Example

defmodule Account do
  def addUp(a, b) do
    a + b
  end
end
IO.puts(Account.addUp(8, 9))

Expected output

17

When to use?

Dividing the program into modules and reusable functions.

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