Chawki

Chawki

What are the differences between Clause, Function and Module?

All seems like the same

Most Liked

NobbZ

NobbZ

A module is a container that contains one or many function. A clause is a branch in a case expression or multiheaded function.

But if you really feel they are the same, could you please point us to the resource that makes you do so? Perhaps we can clarify things a bit for you?

7stud

7stud

hello.exs:

defmodule Calc do
  def go(:show, x) do    # go/2 clause 1
    IO.puts x
  end
  def go(:double, x) do  # go/2 clause 2
    IO.puts 2*x
  end

  def go(x, y, z) do     # go/3
    IO.puts x+y+z
  end
end

defmodule Auto do
  def go(:fast, x) do    # go/2 in another module (=namespace)
    IO.puts "Going #{10 * x} mph!"
  end
end


Auto.go(:fast, 10)
Calc.go(:show, 20)
Calc.go(:double, 20)

Running the code:

~/elixir_programs$ elixir hello.exs
Going 100 mph!
20
40

module: Groups functions under a common name (also known as a namespace) to prevent name clashes with other functions.

function: Takes some input in the form of arguments and performs some task.

clause: Functions can have multiple clauses and elixir pattern matches the function arguments to determine which clause executes. In most languages that people studied before learning elixir, defining multiple function clauses would cause an error.

Chawki

Chawki

I’m cleared thanks

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement