Elixir Functions

From Elixir Wiki
Jump to navigation Jump to search

Elixir Functions[edit]

File:Elixir logo.png

In Elixir, functions are a fundamental part of the language that allow us to encapsulate and group related pieces of code together. They are vital for organizing our code and making it more readable and maintainable. In this article, we will explore the different types of functions in Elixir and learn how to define, call, and work with them effectively.

Function Definitions[edit]

In Elixir, functions are defined using the `def` keyword followed by the function name and a list of parameters. They can also have default values for parameters. Let's look at an example:

```elixir def greet(name \\ "World") do

 "Hello, #{name}!"

end ```

This defines a function named `greet` that takes one optional parameter `name`. If no argument is provided, it defaults to the string "World". The function returns a greeting message.

Calling Functions[edit]

To call a function in Elixir, we use the function name followed by parentheses containing the arguments (if any) separated by commas. Here's an example:

```elixir greet("Alice") ```

This would call the `greet` function with the argument "Alice" and return the string "Hello, Alice!".

Anonymous Functions[edit]

Anonymous functions in Elixir are functions defined without a name. They are often used as arguments to other functions or stored in variables for later use. Here's an example:

```elixir add = fn a, b -> a + b end ```

Here, we define an anonymous function that takes two arguments `a` and `b` and returns their sum. We can then call this anonymous function like any other function:

```elixir add.(3, 4) ```

This would return the result 7.

Higher-Order Functions[edit]

Elixir supports higher-order functions, which are functions that can take other functions as arguments or return functions as results. These functions enable powerful abstractions and functional programming paradigms. Let's see an example:

```elixir numbers = [1, 2, 3] multiply_by_two = fn x -> x * 2 end result = Enum.map(numbers, multiply_by_two) ```

In this example, we define an anonymous function `multiply_by_two` and use it as an argument to the `Enum.map` function. `Enum.map` applies the given function to each element in the list `numbers` and returns a new list with the results. The `result` variable will contain the list `[2, 4, 6]`.

Pattern Matching[edit]

Pattern matching is a powerful feature in Elixir that allows us to define multiple function clauses with different patterns. Elixir matches the arguments passed to a function against these patterns and executes the code block of the matching clause. This enables us to have more flexible and expressive functions. Here's an example:

```elixir def foo(:a) do

 "Found atom :a"

end

def foo(42) do

 "Found the number 42"

end

def foo(_) do

 "Anything else"

end ```

In this example, we have three clauses for the `foo` function. The first one matches when the argument is the atom `:a`, the second one matches when the argument is the number `42`, and the third one matches any other argument. This allows us to handle different cases gracefully.

Conclusion[edit]

Functions are central to Elixir's programming paradigm and provide a powerful way to structure and organize code. In this article, we covered various aspects of functions in Elixir, including function definitions, calling functions, anonymous functions, higher-order functions, and pattern matching. By mastering these concepts, you will be well-equipped to write clean and concise code in Elixir.

Template:Languages