Functions and clauses
A function in Erlang is a set of clauses that share a name and an argument count. Each clause is a pattern for the arguments, an arrow and a body, and the first clause that matches runs. A function is also identified by its name and its arity, and the export list decides which ones the outside world may call.
Multiple clauses
The clauses of one function are separated by semicolons and the last one ends with a full stop. The first clause whose pattern matches the arguments runs, and the rest are skipped, so the specific patterns come first and the general ones last:
describe.erl
-module(describe). -export([main/0, describe/1]). describe(0) -> "zero"; describe(N) when is_integer(N), N > 0 -> "positive"; describe(N) when is_integer(N) -> "negative"; describe(_) -> "not a number". main() -> io:format("~s~n", [describe(0)]), io:format("~s~n", [describe(5)]), io:format("~s~n", [describe(-3)]), io:format("~s~n", [describe(a)]).
erlc describe.erl
erl -noshell -eval 'describe:main(), halt().'
zero positive negative not a number
A guard in the head
A clause may add a when guard after the patterns, and the clause runs only when both the pattern matches and the guard is true. The guard is what keeps a clause from taking a value of the wrong type. Without is_integer(N) the clause would accept the atom a, because the term order puts every atom after every number:
erl
1> a > 0. true 2> is_integer(a). false
That is why describe/1 states the type first and compares second: when is_integer(N), N > 0. A comma in a guard is an and; a semicolon is an or, and the guards lesson has both.
Name and arity
The arity is the number of arguments, and it is part of the function identity: area/2 and area/3 are two different functions that happen to share a name. Each has its own clauses and its own export entry:
geometry.erl
-module(geometry). -export([area/2, area/3]). area(Width, Height) -> Width * Height. area(Width, Height, Depth) -> Width * Height * Depth.
erlc geometry.erl
erl -noshell -eval 'io:format("~p~n", [geometry:area(3, 4)]), io:format("~p~n", [geometry:area(3, 4, 2)]), halt().'
12 24
A call with the wrong number of arguments is not a clause that failed to match: the function does not exist, and the runtime says so with the name and the arity it looked for. The error names the call exactly:
erl
1> geometry:area(3). ** exception error: undefined function geometry:area/1
Export and private
Only exported functions are visible outside the module; everything else is private to it. A private helper keeps the module surface small, and the compiler warns about a private function that nothing calls, which is the cue to delete it. This module exports two functions and keeps double/1 for itself:
shape.erl
-module(shape). -export([area/2, perimeter/2]). area(Width, Height) -> Width * Height. perimeter(Width, Height) -> double(Width) + double(Height). double(X) -> X * 2.
erlc shape.erl
erl -noshell -eval 'io:format("~p~n", [shape:area(3, 4)]), io:format("~p~n", [shape:perimeter(3, 4)]), halt().'
12 14
A call to shape:double(3) from outside fails with undefined function shape:double/1, the same message as a module that does not exist, because the runtime only knows the exports. The modules lesson covers the export list together with -import and the call into another module.
The semicolon and the full stop
A semicolon says another clause follows; a full stop ends the function. Ending a non-final clause with a full stop starts a new function with the same name and arity, which the compiler refuses. A page that shows the mistake as a quiz question gets the line that names it:
bad.erl:5:1: function f/1 already defined is what the compiler answers for a first clause that ends with . instead of ;. The fix is one character, and the message names the function and the line without a stack trace.
Anonymous functions
fun ... end builds a function value with no name. It is the usual way to hand behaviour to another function, and the value is called like any other function. The full lesson is Funs: anonymous functions; here is the shape only:
erl
1> F = fun(X) -> X * 2 end, F(21). 42
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 does this module print?
-module(q3). -export([main/0]). area(Width, Height) -> Width * Height. area(Width, Height, Depth) -> Width * Height * Depth. main() -> io:format("~p~n", [area(3, 4)]), io:format("~p~n", [area(3, 4, 2)]).Answer
12then24The two-argument call selects
area/2and the three-argument call selectsarea/3. -
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.
-
What does this module print?
-module(q4). -export([main/0, perimeter/2]). perimeter(Width, Height) -> double(Width) + double(Height). double(X) -> X * 2. main() -> io:format("~p~n", [perimeter(3, 4)]).Answer
14A private function is callable inside its module, and (3 * 2) + (4 * 2) is 14.
-
Why does
describe/1testis_integer(N)beforeN > 0?Answer
Without the type test an atom also passes, because atoms follow numbers in the term order
The comparison is defined for every term, so the type test is what limits the clause to numbers.
-
Which character separates two clauses of the same function?
Answer
A semicolon
Semicolons separate clauses; the last one ends with the full stop.
-
What does this module print?
-module(q1). -export([main/0, describe/1]). describe(0) -> zero; describe(N) when N > 0 -> positive; describe(_) -> other. main() -> io:format("~p~n", [describe(0)]).Answer
zeroThe first clause matches 0 directly, so it runs before the guarded one.
-
What does this module print?
-module(q2). -export([main/0]). area(Width, Height) -> Width * Height. main() -> io:format("~p~n", [area(3, 4)]).Answer
12A private function may be called inside its own module, and 3 * 4 is 12.
-
A module has a first clause
f(0) -> zero.and thenf(N) -> N.What does the compiler say?Answer
function f/1 already definedThe full stop ended the function, so the second clause defines
f/1a second time.
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.
Автор статьи: Arthur Isaev