Functions and clauses

Contents
Multiple clauses
Name and arity
Export them
Anonymous functions

Multiple clauses

A function is a set of clauses with the same name and arity. The first clause whose pattern matches the arguments runs, and the rest are skipped.

Put the specific patterns first and the general ones last, in the same order a case expression would use.

describe(0) -> "zero";

describe(N) when N > 0 -> "positive";

describe(N) -> "negative".

Name and arity

The arity is the argument count, and it is part of the function identity. describe/1 and describe/2 are two different functions that happen to share a name.

The export list uses the name and arity pair, so main/0 and main/1 would be separate exports.

-export([describe/1, area/2]).

area(W, H) -> W * H.

Export them

Only exported functions are visible outside the module. Everything else is private, which keeps the module surface small and the refactors safe.

The compiler warns about unused and unexported functions, and the warning is your cue to clean the file.

-module(shape).

-export([area/2]).

% private helper: no export needed

double(X) -> X * 2.

Anonymous functions

The fun keyword builds a function without a name. It appears inline and is the usual way to hand behavior to another function.

Lesson 21 gives funs the full lesson; here is the shape only.

1> fun(X) -> X * 2 end.

#Fun

Next: Pattern matching

Article author: Arthur Isaev

Related articles
Erlang
Erlang course: zero to hero in 50 lessons
What is Erlang and where it runs
Install Erlang and check the version
erl: the interactive shell
Your first Erlang script
Variables and single assignment
Numbers
Atoms
Strings and binaries
Truthiness: only false and nil
Conditions: case and if
Recursion replaces loops
Lists
Tuples
Maps
Functions and clauses
Pattern matching
Guards
Records
Exceptions: try and catch
Modules, exports and imports
Funs: anonymous functions
Higher-order functions
Keyword lists
Erlang idioms and clean code
Rebar3 builds
Testing with EUnit
Debugging
Encodings and Unicode
Time and date
JSON and term formats
Processes and message passing
Spawn and links
Monitors and exits
GenServer
Supervisors
OTP applications
Files and IO
Sockets
HTTP clients
io:format and output
Let it crash: error handling
ETS tables
Timers and timeouts
Code loading
Distributed Erlang
Capstone: todo CLI
Capstone: TCP server
Performance
Modern Erlang tour
Hero roadmap: the whole course on one page
Install Erlang on Windows 11
Install Erlang on Ubuntu
Install Erlang on Rocky Linux
Install Erlang on macOS
Install Erlang on FreeBSD
Erlang in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: