Erlang (programming language)/Tutorials/Functions

From Citizendium
< Erlang (programming language)‎ | Tutorials
Revision as of 19:55, 19 April 2008 by imported>Eric Evers (New page: =Syntax of functions= Functions are defined by the domain of the arguments and the number of arguemnts. A function ends with a period. A function defined over differnt domains are separat...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Syntax of functions

Functions are defined by the domain of the arguments and the number of arguemnts. A function ends with a period. A function defined over differnt domains are separated by semicolons. A fact function gives an answer that is sensitive to the domain of the input. With strings it gives a fact. With counting numbers it gives the factorial function.

fact("aloha") -> "Aloha is a greating";
fact(String) when is_a_list(String) -> 
    "no fact is known about " ++ String;
fact(0) -> 1;
fact(N) when is_integer(N) and (N > 0) -> 
    fact(N-1)*N;
fact(N) when N < 0 -> error.