Elixir Modules and Functions

From Elixir Wiki
Jump to navigation Jump to search

Elixir Modules and Functions[edit]

Elixir is a functional programming language that runs on the Erlang Virtual Machine (BEAM) and provides developers with a powerful and flexible syntax. One of the key features of Elixir is its module and function system, which allows for the organization and structuring of code.

Modules[edit]

Modules in Elixir are containers for functions, types, and other code. They serve as a way to group related functionality together and provide a namespace for the functions and types defined within them. Modules are defined using the `defmodule` keyword followed by the module name and optional do-end block.

Here is an example of a simple module definition: ```elixir defmodule Math do

 def sum(a, b) do
   a + b
 end

end ```

In the example above, we define a module named `Math` which contains a single function `sum`. The `sum` function takes two arguments `a` and `b` and returns their sum.

Functions[edit]

Functions in Elixir are defined within modules and can be invoked by using the module name followed by the function name and the necessary arguments. Functions can have multiple clauses, each defined with the `def` keyword. Elixir uses pattern matching to determine which clause to invoke based on the provided arguments.

Here is an example of a module with multiple function clauses: ```elixir defmodule Calculator do

 def add(a, b) do
   a + b
 end
 def add(a, b, c) do
   a + b + c
 end

end ```

In the example above, the `Calculator` module contains two function clauses for the `add` function. The first clause takes two arguments `a` and `b` and returns their sum. The second clause takes three arguments `a`, `b`, and `c`, and returns their sum.

Built-in Modules[edit]

Elixir provides a set of built-in modules that offer various functionalities for common tasks. These modules can be used directly without the need for additional imports. Some of the commonly used built-in modules include:

- `Kernel` - Provides core functions and macros used throughout Elixir. - `Enum` - Offers a wide range of functions for working with lists and enumerables. - `List` - Provides functions specifically for working with lists. - `String` - Offers functions for string manipulation and transformation. - `File` - Provides functions for interacting with the file system.

Anonymous Functions[edit]

In addition to named functions, Elixir also supports anonymous functions. Anonymous functions are defined using the `fn` keyword followed by the function body and an optional `->` separator. They can be assigned to variables or passed as arguments to other functions.

Here is an example of an anonymous function: ```elixir sum = fn (a, b) -> a + b end IO.puts sum.(2, 3) #=> 5 ```

In the example above, we define an anonymous function `sum` that takes two arguments `a` and `b` and returns their sum. We then pass arguments `2` and `3` to the `sum` function using the dot notation `sum.(2, 3)`.

Conclusion[edit]

Understanding modules and functions is essential for writing clean and organized code in Elixir. Modules allow you to group related functionality together, while functions provide the logic and behavior within those modules. Take advantage of the powerful features of Elixir's module and function system to write efficient and maintainable code.