Editing
Function
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Function == 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 === 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 === 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 === 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 === 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 === 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 === 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 === 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.
Summary:
Please note that all contributions to Elixir Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Elixir Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information