Revision as of 10:37, 7 March 2012

A function with the same name and the same number of parameters can be defined several times, where through pattern matching, the clause chosen depends on the parameters and arguments passed to it. Consider the following example:

factorial(1) -> 1;
factorial(N) -> N * factorial(N-1).

if the argument in the function call is not 1, the second clause is automatically chosen, and the argument is bound to N. If the argument is 1, the first clause is chosen, and 1 is returned.

Technically speaking, formal parameters are also patterns - it's just that they never fail to match a value. As a "side effect" of the successful match, the formal parameter is bound to the value it is being matched against.
Patterns such as formal parameters that never fail to match are said to be irrefutable, in contrast to refutable patterns that may fail to match. Consider the following function:

factorial(N) ->
...

In this function, the formal parameter is N. If you execute the function factorial with ANY value as parameter, N will match it and become bound. In this case the pattern N is irrefutable. However, we know that the factorial function can only take positive integers larger than 0 as input.

factorial(N) when is_integer(N), (N > 0) ->
...

In this case, N can only be bound if it is integer and larger than 0. Now the pattern is refutable. Technically, we have placed two guards that prevents N from being bound: