Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Functions

functions.livemd

Functions

Modules

A module is a set of functions.

defmodule Mathematics do

  def sum(x, y) do # arity is 2
    x + y
  end

  def multiply(x, y) do # arity is 2
    x * y
  end

  def multiply(x, y, z) do # arity is 3
    x * y * z
  end
  
end

(Named) Functions

Mathematics.sum(3,4)
Mathematics.multiply(7,8)
Mathematics.multiply(10, 3, 2)

Variables

x = 1
x = 2
x

Anonymous Functions

fn(x) -> x*2 end
f = fn(x) -> x*3 end
f.(3)
g = fn(x,y,z) -> (x+y)*z end
g.(2,3,10)
x = 10
h = fn(y) -> x+y end
x = 20
h.(20)

Values

# integer numbers

1
# floating point numbers

3.5
"strings"
true
false
:height
:weight
0b0110
0b011010100101010
0xCAFE
0o7765
57263845687432658346528734658236458734687565726384568743265834652873465823645873468756572638456874326583465287346582364587346875657263845687432658346528734658236458734687565726384568743265834652873465823645873468756572638456874326583465287346582364587346875657263845687432658346528734658236458734687565726384568743265834652873465823645873468756572638456874326583465287346582364587346875657263845687432658346528734658236458734687565726384568743265834652873465823645873468756572638456874326583465287346582364587346875657263845687432658346528734658236458734687565726384568743265834652873465823645873468756
1.57263845687432658346528734658236458734687565726384568743265834652873465823645873468756572638456874326583465287346582364587346875657263845687432658346528734658236458734687565726384568743265834652873465823645873468756572638456874326583465287346582364587346875657263845687432658346528734658236458734687565726384568743265834652873465823645873468756572638456874326583465287346582364587346875657263845687432658346528734658236458734687565726384568743265834652873465823645873468756572638456874326583465287346582364587346875657263845687432658346528734658236458734687565726384568743265834652873465823645873468756