1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- This document was generated using DocBuilder 3.3.3 -->
<HTML>
<HEAD>
<TITLE>Functions</TITLE>
<SCRIPT type="text/javascript" src="../../doc/erlresolvelinks.js">
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#FF00FF"
ALINK="#FF0000">
<CENTER>
<A HREF="http://www.erlang.se"><IMG BORDER=0 ALT="[Ericsson AB]" SRC="min_head.gif"></A>
</CENTER>
<A NAME="5"><!-- Empty --></A>
<H2>5 Functions</H2>
<A NAME="syntax"><!-- Empty --></A><A NAME="5.1"><!-- Empty --></A>
<H3>5.1 Function Declaration Syntax</H3>
<P>A <STRONG>function declaration</STRONG> is a sequence of function
clauses separated by semicolons, and terminated by period (.).
<P>A <STRONG>function clause</STRONG> consists of a clause head and a
clause body, separated by <CODE>-></CODE>.
<P>A clause <STRONG>head</STRONG> consists of the function name, an
argument list, and an optional guard sequence
beginning with the keyword <CODE>when</CODE>.
<PRE>
Name(Pattern11,...,Pattern1N) [when GuardSeq1] ->
Body1;
...;
Name(PatternK1,...,PatternKN) [when GuardSeqK] ->
BodyK.
</PRE>
<P>The function name is an atom. Each argument is a pattern.
<P>The number of arguments <CODE>N</CODE> is the <STRONG>arity</STRONG> of
the function. A function is uniquely defined by the module name,
function name and arity. That is, two functions with the same
name and in the same module, but with different arities are two
completely different functions.
<P>A function named <CODE>f</CODE> in the module <CODE>m</CODE> and with arity
<CODE>N</CODE> is often denoted as <CODE>m:f/N</CODE>.
<P>A clause <STRONG>body</STRONG> consists of a sequence of expressions
separated by comma (,):
<PRE>
Expr1,
...,
ExprN
</PRE>
<P>Valid Erlang expressions and guard sequences are described in
<A HREF="expressions.html">Erlang Expressions</A>.
<P>Example:
<PRE>
fact(N) when N>0 -> % first clause head
N * fact(N-1); % first clause body
fact(0) -> % second clause head
1. % second clause body
</PRE>
<A NAME="eval"><!-- Empty --></A><A NAME="5.2"><!-- Empty --></A>
<H3>5.2 Function Evaluation</H3>
<P>When a function <CODE>m:f/N</CODE> is called, first the code for
the function is located. If the function cannot be found, an
<CODE>undef</CODE> run-time error will occur. Note that the function
must be exported to be visible outside the module it is defined
in.
<P>If the function is found, the function clauses are scanned
sequentially until a clause is found that fulfills the following
two conditions:
<P>
<OL>
<LI>
the patterns in the clause head can be successfully
matched against the given arguments, and
</LI>
<LI>
the guard sequence, if any, is true.
</LI>
</OL>
<P>If such a clause cannot be found, a <CODE>function_clause</CODE>
run-time error will occur.
<P>If such a clause is found, the corresponding clause body is
evaluated. That is, the expressions in the body are evaluated
sequentially and the value of the last expression is returned.
<P>Example: Consider the function <CODE>fact</CODE>:
<PRE>
-module(m).
-export([fact/1]).
fact(N) when N>0 ->
N * fact(N-1);
fact(0) ->
1.
</PRE>
<P>Assume we want to calculate factorial for 1:
<PRE>
1> <STRONG>m:fact(1).</STRONG>
</PRE>
<P>Evaluation starts at the first clause. The pattern <CODE>N</CODE> is
matched against the argument 1. The matching succeeds and
the guard (<CODE>N>0</CODE>) is true, thus <CODE>N</CODE> is bound to 1 and
the corresponding body is evaluated:
<PRE>
<STRONG>N * fact(N-1)</STRONG> => (N is bound to 1)
<STRONG>1 * fact(0)</STRONG>
</PRE>
<P>Now <CODE>fact(0)</CODE> is called and the function clauses are
scanned sequentially again. First, the pattern <CODE>N</CODE> is
matched against 0. The matching succeeds, but the guard
(<CODE>N>0</CODE>) is false. Second, the pattern 0 is matched against
0. The matching succeeds and the body is evaluated:
<PRE>
<STRONG>1 * fact(0)</STRONG> =>
<STRONG>1 * 1</STRONG> =>
<STRONG>1</STRONG>
</PRE>
<P>Evaluation has succeed and <CODE>m:fact(1)</CODE> returns 1.
<P>If <CODE>m:fact/1</CODE> is called with a negative number as
argument, no clause head will match. A <CODE>function_clause</CODE>
run-time error will occur.<A NAME="5.3"><!-- Empty --></A>
<H3>5.3 Tail recursion</H3>
<P>If the last expression of a function body is a function call,
a <STRONG>tail recursive</STRONG> call is done so that no system
resources for example call stack are consumed. This means
that an infinite loop can be done if it uses tail recursive
calls.
<P>Example:
<PRE>
loop(N) ->
io:format("~w~n", [N]),
loop(N+1).
</PRE>
<P>As a counter-example see the factorial example above
that is not tail recursive since a multiplication is done
on the result of the recursive call to <CODE>fact(N-1)</CODE>.<A NAME="5.4"><!-- Empty --></A>
<H3>5.4 Built-In Functions, BIFs</H3>
<P><STRONG>Built-in functions</STRONG>, BIFs, are implemented in C code in
the runtime system and do things that are difficult or impossible
to implement in Erlang. Most of the built-in functions belong
to the module <CODE>erlang</CODE> but there are also built-in functions
belonging to a few other modules, for example <CODE>lists</CODE> and
<CODE>ets</CODE>.
<P>The most commonly used BIFs belonging to <CODE>erlang</CODE> are
<STRONG>auto-imported</STRONG>, they do not need to be prefixed with
the module name. Which BIFs are auto-imported is specified in
<CODE>erlang(3)</CODE>. For example, standard type conversion BIFs like
<CODE>atom_to_list</CODE> and BIFs allowed in guards can be called
without specifying the module name. Examples:
<PRE>
1> <STRONG>size({a,b,c}).</STRONG>
3
2> <STRONG>atom_to_list('Erlang').</STRONG>
"Erlang"
</PRE>
<P>Note that normally it is the set of auto-imported built-in
functions that is referred to when talking about 'BIFs'.<CENTER>
<HR>
<SMALL>
Copyright © 1991-2006
<A HREF="http://www.erlang.se">Ericsson AB</A><BR>
</SMALL>
</CENTER>
</BODY>
</HTML>
|