8.6 Function Calls

  1. ExprF(Expr1,...,ExprN)
  2. ExprM:ExprF(Expr1,...,ExprN)

In the first form of function calls, ExprM:ExprF(Expr1,…,ExprN), each of ExprM and ExprF must be an atom or an expression that evaluates to an atom. The function is said to be called by using the fully qualified function name. This is often referred to as a remote or external function call.

Example:

  1. lists:keysearch(Name, 1, List)

In the second form of function calls, ExprF(Expr1,…,ExprN), ExprF must be an atom or evaluate to a fun.

If ExprF is an atom, the function is said to be called by using the implicitly qualified function name. If the function ExprF is locally defined, it is called. Alternatively, if ExprF is explicitly imported from the M module, M:ExprF(Expr1,…,ExprN) is called. If ExprF is neither declared locally nor explicitly imported, ExprF must be the name of an automatically imported BIF.

Examples:

  1. handle(Msg, State)
  2. spawn(m, init, [])

Examples where ExprF is a fun:

  1. 1> Fun1 = fun(X) -> X+1 end,
  2. Fun1(3).
  3. 4
  4. 2> fun lists:append/2([1,2], [3,4]).
  5. [1,2,3,4]
  6. 3>

Notice that when calling a local function, there is a difference between using the implicitly or fully qualified function name. The latter always refers to the latest version of the module. See Compilation and Code Loading and Function Evaluation.

Local Function Names Clashing With Auto-Imported BIFs

If a local function has the same name as an auto-imported BIF, the semantics is that implicitly qualified function calls are directed to the locally defined function, not to the BIF. To avoid confusion, there is a compiler directive available, -compile({no_auto_import,[F/A]}), that makes a BIF not being auto-imported. In certain situations, such a compile-directive is mandatory.

Warning

Before OTP R14A (ERTS version 5.8), an implicitly qualified function call to a function having the same name as an auto-imported BIF always resulted in the BIF being called. In newer versions of the compiler, the local function is called instead. This is to avoid that future additions to the set of auto-imported BIFs do not silently change the behavior of old code.

However, to avoid that old (pre R14) code changed its behavior when compiled with OTP version R14A or later, the following restriction applies: If you override the name of a BIF that was auto-imported in OTP versions prior to R14A (ERTS version 5.8) and have an implicitly qualified call to that function in your code, you either need to explicitly remove the auto-import using a compiler directive, or replace the call with a fully qualified function call. Otherwise you get a compilation error. See the following example:

  1. -export([length/1,f/1]).
  2.  
  3. -compile({no_auto_import,[length/1]}). % erlang:length/1 no longer autoimported
  4.  
  5. length([]) ->
  6. 0;
  7. length([H|T]) ->
  8. 1 + length(T). %% Calls the local function length/1
  9.  
  10. f(X) when erlang:length(X) > 3 -> %% Calls erlang:length/1,
  11. %% which is allowed in guards
  12. long.

The same logic applies to explicitly imported functions from other modules, as to locally defined functions. It is not allowed to both import a function from another module and have the function declared in the module at the same time:

  1. -export([f/1]).
  2.  
  3. -compile({no_auto_import,[length/1]}). % erlang:length/1 no longer autoimported
  4.  
  5. -import(mod,[length/1]).
  6.  
  7. f(X) when erlang:length(X) > 33 -> %% Calls erlang:length/1,
  8. %% which is allowed in guards
  9.  
  10. erlang:length(X); %% Explicit call to erlang:length in body
  11.  
  12. f(X) ->
  13. length(X). %% mod:length/1 is called

For auto-imported BIFs added in Erlang/OTP R14A and thereafter, overriding the name with a local function or explicit import is always allowed. However, if the -compile({no_auto_import,[F/A]) directive is not used, the compiler issues a warning whenever the function is called in the module using the implicitly qualified function name.