Elixir Functions - Elixir

What are different types of functions in Elixir?

A function is a set of statements that are structured together to perform a specific task. Functions in programming work similar to function in math. Functions generate output based on the input provided. There are 2 types of functions in elixir:

1. Anonymous function

Anonymous functions are defined using fn..end construct and these functions are sometimes also called as lambdas. They are used by assigning them to variable names.

2. Named function

Named functions are defined using def keyword and these are native functions provided in Elixir.

Anonymous functions

As the name implies, anonymous function has no name and are frequently passed to other functions. fn and end keywords are used to define an anonymous function in Elixir. Within these, any number of parameters and function bodies can be defined separated by ->. For example,

While running above program, it produces following result:

Remember that these functions are not called like named functions. There is a '.' between the function name and its arguments.

Using the Capture operator

Capture operator can be used to define these functions. This is actually a shortcut method to create functions. To define the above sum function using the capture operator,

While running above program, it produces following result:

In the shortcut version, parameters are not named but are available to us as &1, &2, &3, and so on.

Pattern matching functions

Pattern matching is not only limited to variables and data structures. It can also be used to make our functions polymorphic. For example, If we want to declare a function that can either take 1 or 2 inputs(within a tuple) and prints them to the console,

While running above program, it produces following result:

Named functions

Functions are defined with names so that it is easy to refer them later. Named functions are defined within a module using def keyword and are always defined in a module. To call named functions we need to reference them using their module name.

Below is the syntax for named functions:

Now try defining your own named function sum within module Math.

While running above program, it produces following result:

For 1-liner functions, there is a shorcut notation to define these functions, using do:. For example:

While running above program, it produces following result:

Private functions

Elixir gives an ability to define private functions which can be accessed from within the module in which they are defined. To define a private function, use defp instead of def. For example:

While running above program, it produces following result:

When we just try to explicitly call phrase function, using Greeter.phrase(), it'll raise an error.

Default arguments

If you want a default value for an argument we use the argument \\ value syntax:

While running above program, it produces following result:

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Elixir Topics