Passing list values to a function

In elixir, everything is “discarded” except the last line of the function. For example:

def my_func(param1, param2) do
  Database.query(param1) # returns list of users [%User{} ...]
  UserProfile.update!(param1, param2) # returns %UserProfile{} struct
  1 + 1
end

The only thing my_func is going to return is 2. The first two function calls will still happen - leading to potential “side_effects” (like updating a row in the database), but they have no effect on the return value of my_func. Only the last line is returned. If you want to return the result of several function in combination, you use the pipeline operator:

def my_func(user_id, new_email) do
  User.get_user!(user_id)
  |> User.update!(new_email)
  |> Map.get(:email)
end

This function will fetch a User from the db, pass that user along to the next function where it gets updated in the db, and finally get the email from out of our updated %User{} struct.