English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Erlang is a well-known functional programming language, so you will see many discussions about how functions work in Erlang. This chapter introduces how to use functions in Erlang to complete all operations.
The syntax for function declaration is as follows:
FunctionName(Pattern1… PatternN) -> Body;
Here,
FunctionName − The function name is an atom (uniqueness)
Pattern1... PatternN − Each parameter is a pattern. The number of parameters N is the number of parameters of the function. The function name is unique by module, function name, and defined parameter count. That is, two functions with the same name in the same module but with different parameter counts are also two different functions.
Body − A clause body is a sequence of expressions separated by commas (,)
The following program is a simple example of using a function -
-module(helloworld). -export([add/2,start/0]). add(X,Y) -> Z = X+Y, io:fwrite("~w~n",[Z]). start() -> add(5,6).
The following points should be noted about the above program-
We define two functions, one is add, which needs 2 one parameter, and the other function is start;
Both functions are defined using the export function. If we do not do this, the function cannot be used;
A function can be called within another function. In this case, the start function will call the add function;
The output result of the above code is -
11
Anonymous function
An anonymous function is a function that does not have any associated name. In Erlang, there are tools that can be used to define anonymous functions. The following program is an example of an anonymous function.
-module(helloworld). -export([start/0]). start() -> Fn = fun() -> io:fwrite("Anonymous Function") end, Fn().
For the above example, the following points should be noted -
Anonymous functions are defined using the fun() keyword
The function is assigned to a variable named Fn
The function is called by variable name
The output result of the above code is -
Anonymous Function
Function with multiple parameters
Erlang functions can be defined with zero or more parameters. Function overloading is also allowed; you can define a function with the same name, as long as they have a different number of parameters. In the following example, each function definition of the demo function is defined using multiple parameters.
-module(helloworld). -export([add/2,add/3,start/0]). add(X,Y) -> Z = X+Y, io:fwrite("~w~n",[Z]). add(X,Y,Z) -> A = X+Y+Z, io:fwrite("~w~n",[A]). start() -> add(5,6), add(5,6,6).
In the above program, we define the add function twice. But the definition of the first add function uses two parameters, and the definition of the second add function uses three parameters.
The output result of the above code is -
11 17
Function uses sequence
In Erlang, functions also have the ability to use protection sequences. These are not much; they are just expressions that only run when evaluated (computed) as true.
The syntax for using the protection sequence function is shown in the following program.
FunctionName(Pattern1… PatternN) [when GuardSeq1]-> Body;
Here,
FunctionName − The function name is an atom (uniqueness)
Pattern1... PatternN − Each parameter is a pattern. The number of parameters N is the number of parameters of the function. The function name is unique by module, function name, and defined parameter count. That is, two functions with the same name in the same module but with different parameter counts are also two different functions.
Body − A clause body is a sequence of expressions separated by commas (,)
GuardSeq1 − When the function is called, it gets the expression to be evaluated.
The following program is a simple example of a function using a guard sequence.
-module(helloworld). -export([add/1,start/0]). add(X) when X>3 -> io:fwrite("~w~n",[X]). start() -> add(4).
The output result of the above code is -
4
If the add function is called as add(3),this program will produce an error.