Pass arguments into Elixir functions and understand arity

Kyle Gill
InstructorKyle Gill
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 4 years ago

The syntax to pass arguments into functions is akin to many other languages. Elixir functions are uniquely recognized when running based off of the number of arguments passed in (a term called arity). This means you can define functions with the same name, so long as a different number of arguments is passed in.

Arity is a key concept to be aware of when reading documentation and writing your own code.

Additional resources:

Kyle Gill: [0:00] You can pass arguments into Elixir functions by including the names of the parameters you'd like to access in parentheses next to the function name. In this hello function, you can use Elixir string interpolation with the # sign to display the full message. You can now run the project in an interactive shell and call the hello method by passing in a value.

[0:19] If you try to call the hello method without any parameters, you'll get an error. Each function in Elixir is unique, based on its name and its arity, meaning the number of arguments passed in, which in this case is . Your function defined in code has an arity of 1.

[0:36] You could allow the function to be called without any arguments in two ways. You can provide a default value for the parameter in the function declaration with \ and a value, or you can define a new function with the same name and an arity of .

[0:52] After running the shell again or recompiling the code, you can call hello with or without an argument.