Pass arguments into Elixir functions and understand arity

Kyle Gill
InstructorKyle Gill
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 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.

egghead
egghead
~ 39 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today