Functions and clauses
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.
-module(describe).
-export([describe/1]).
describe(0) -> "zero";
describe(N) when N > 0 -> "positive";
describe(_) -> "negative".
erlc describe.erl
erl
1> describe:describe(0).
"zero"
2> describe:describe(5).
"positive"
3> describe:describe(-3).
"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.
-module(geometry).
-export([area/2, area/3]).
area(W, H) -> W * H.
area(W, H, Depth) -> W * H * Depth.
erlc geometry.erl
erl
1> geometry:area(3, 4).
12
2> geometry:area(3, 4, 2).
24
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, perimeter/2]).
area(W, H) -> W * H.
perimeter(W, H) -> double(W) + double(H).
% private helper: no export needed
double(X) -> X * 2.
erlc shape.erl
erl
1> shape:area(3, 4).
12
2> shape:perimeter(3, 4).
14
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.
erl
1> F = fun(X) -> X * 2 end, F(21).
42
Next: Pattern matching
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What makes one function clause different from the next?
Answer
The pattern it matches
Clauses share a name and arity, and the pattern decides which one matches the arguments.
-
Which clause runs when several patterns match?
Answer
The first matching clause
Clauses are tried in order, so the specific patterns must come before the general ones.
-
What is the arity of
area(W, H)?Answer
2The arity is the number of arguments, and this function takes two.
-
Are
area/2andarea/3the same function?Answer
No, the arity is part of the function identity
Erlang identifies a function by name and arity, so the two are separate.
-
How is a function exported?
Answer
With
-export([area/2]).The
-exportattribute lists the name and arity pairs the outside world may call. -
Which functions are visible outside a module?
Answer
Only the exported ones
A function that is not exported is private to its module.
-
What does the compiler do with an unused private function?
Answer
Warns about it
The warning about an unused function is the cue to remove dead code.
-
Which keyword builds an anonymous function?
Answer
funfun(...) -> ... endbuilds a function value without a name. -
What does
F = fun(X) -> X * 2 end, F(21).return?Answer
42Fis bound to the fun, and calling it with 21 doubles the number. -
What does
describe:describe(-3).return with the module above?Answer
"negative"The first clause wants 0 and the guarded clause wants a positive number, so the last clause answers.
The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev