Function

From Elixir Wiki
Jump to navigation Jump to search

Function[edit]

A function in Elixir is a fundamental building block of the language. It is a reusable piece of code that can perform a specific task. Functions are responsible for defining behavior and encapsulating logic within Elixir programs. This article will explore the various aspects and features of functions in Elixir.

Syntax[edit]

The syntax for defining a function in Elixir follows the convention of `def` keyword, followed by the function name, parameters, and the function body. Here's an example:

```elixir def my_function(parameter1, parameter2) do

 # Function body

end ```

Parameters[edit]

Functions in Elixir can have zero or more parameters. Parameters allow us to pass values to the function and use them within its body. Elixir supports both positional and keyword parameters. Positional parameters are identified by their position in the parameter list, while keyword parameters are identified by their name. Here's an example of a function with both positional and keyword parameters:

```elixir def greet(name, age: 20) do

 IO.puts("Hello, #{name}! You are #{age} years old.")

end ```

Return Values[edit]

A function in Elixir can return a value using the `do` block. The last expression within the block is considered the return value of the function. However, it is also possible to use the `return` keyword for early exits from a function. Here's an example:

```elixir def add(a, b) do

 if a > 0 and b > 0 do
   return a + b
 end
 # This line will be reached only if the condition fails
 IO.puts("Invalid input.")
 nil

end ```

Function Calls[edit]

To call a function in Elixir, we use the function name followed by parentheses. If the function has parameters, we pass the arguments within the parentheses. Here's an example:

```elixir result = add(10, 5) ```

Anonymous Functions[edit]

In addition to named functions, Elixir also supports anonymous functions. Anonymous functions are functions without a name and are typically used as values or passed as arguments to other functions. Here's an example of an anonymous function:

```elixir double = fn x -> x * 2 end result = double.(10) ```

Higher-Order Functions[edit]

Elixir allows the usage of higher-order functions, where functions can accept other functions as arguments and/or return functions as results. This powerful concept enables the use of functional programming principles and allows for creating more flexible and modular code. Check out the article on Higher-Order Functions for more information.

Conclusion[edit]

Functions are an essential part of Elixir programming. They provide a way to encapsulate logic, promote reusability, and enhance the overall structure of programs. By understanding the syntax, parameters, return values, and function calls, developers can effectively leverage functions to build robust and maintainable Elixir applications.

For more advanced topics related to functions, such as function overloading and function specifications, refer to the respective articles on the Elixir wiki.