Write a New Function Inside of an Elixir Module

Kyle Gill
InstructorKyle Gill

Share this video with your friends

Send Tweet
Published 2 years ago
Updated a year ago

Elixir code is organized into groups of functions called modules. You can create your own modules and built on top of predefined modules that are a part of Elixir by default.

Additional resources:

Kyle Gill: [0:00] Almost all Elixir code is organized in modules. Modules is a group of related functions. Elixir has some modules like String and List built in and you can also define your own.

[0:10] Inside a defmodule declaration, you can define functions with the def keyword, using a function name and wrapping the code in the do and end keywords. Because Elixir supports implicit returns, you can write a string as the last line of the function, and it'll be returned when the function is called.

[0:25] To run the project, you can use the interactive Elixir shell and use -S mix as a parameter to compile your project and make your module available in the shell. The full command is iex -S mix. Then you can call a method on your module, HelloWorld.hello, and you can include or leave off parentheses.