LauraBeatris

LauraBeatris

How does implict return work in functions?

I learned that Elixir has an implicit return in functions, so we don’t need to use the return keyword in order to return a value if it’s the last one of the block.

  def create_deck do
    ["Pikachu", "Bulbasaur", "Piplup"] # Will return right away
  end

But how does that work? And if the function has nothing to return?

Marked As Solved

bglusman

bglusman

Not only does it have implicit return, there is no alternative, no return keyword in Elixir.

Functions in elixir (and erlang) always return some value. That value can be simply nil or :ok or whatever the output of the last statement executed in the function, but it will have a return value. If you only care about the side effects of calling the function, you may ignore its return value at the call site.

Note that if, case and cond all return the last value from their executed path as well.

The concept of referential transparency has some relevance here; though elixir doesn’t have strict referential transparency because of side effects, when there’s no explicit or implicit message passing to other processes or IO, I believe it does. Just thought worth mentioning, you asked “how does that work”, and depending on what sense you meant that in, there are a lot of answers, but it works great!

Also Liked

jtsummers

jtsummers

This is a consequence of Elixir (like Erlang) being expression-oriented, not statement oriented. Nearly everything in the language is an expression which evaluates to a value. This is in contrast with statement-oriented languages like C, Java, and others.

This has a few implications:

if and case can be used on the right hand side of = in Elixir, but not in C. Here’s an example:

a = if 1 == 2 do
      "huh"
    else
      "expected"
    end

In C that wouldn’t work since if returns nothing, you’d need to use the ternary expression to achieve a similar result. That choice of term is important. In every language (that I know of) expressions and statements are distinct.

Qqwy

Qqwy

TypeCheck Core Team

To add on what the others have already said:

Expressions that have “nothing” to return usually return nil or sometimes :ok. An example is for instance if false do ... end (which returns nil because there is no else-block) and IO.puts/1 (which returns :ok).

dimitarvp

dimitarvp

Then it implicitly returns nil (which is actually a syntactic sugar for the atom :nil, same as true and false are syntactic sugar for :true and :false). Try this:

defmodule Nothing do
  def nothing()
  end
end

And then invoke Nothing.nothing() in iex. You’ll get nil.

kokolegorille

kokolegorille

Then You call it a method :slight_smile:

It’s all about function composition. If it returns nothing, it is most likely a procedure, then it’s not possible to compose with it.

As simple as it is, a function should look like this, and pure function always returns the same output for the same input. You can compose with the powerful |>

Input → Output |> Input → Output |> Input → Output

And coming from Typescript, You will see it’s important to have clear input and output types. Functions are like interfaces.

This video helped me to switch from OOP to FP, because it’s better not to compare with what You already know. In fact, You need to unlearn :slight_smile:

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement