What do the characters mean in the parameter of a function in Elixir?

Asked

Viewed 56 times

2

For example:

defmodule Math do   
   def add(a \\ 2, b) do
      a + b   
   end 
end

What those two bars mean?

1 answer

2


This means a default value for the parameter passed, so in the example given, if no value is passed to the variable a she by default will have a match with the number 2:

defmodule Math do   
   def add(a \\ 2, b) do
     a + b   
   end 
end

If we test the function on iex:

iex (1)> c("math.ex")

iex (2)> Math.add(1, 8) # add/2, pois estamos fazendo o match de 1 com `a`
9

iex (3)> Math.add(8) # add/1, pois `a` está com match padrão a 2.
10

Reference to Stackoverflow Question: https://stackoverflow.com/questions/34563884/what-does-the-double-backslash-mean-in-a-function-parameter-in-elixir

Browser other questions tagged

You are not signed in. Login or sign up in order to post.