Functions with different number or arguments - are they the same?

Hello, I have a simple question

this fonction

  def world(conn, _params) do
    render(conn, "world.html")
  end

it is the same than this function (for the compiler) ?

  def world(conn) do
    render(conn, "world.html")
  end

Thx :slight_smile:

1 Like

Not the same as these two have different arities.

Identifying functions and documentation

Functions in Elixir are identified by both their name and their arity. The arity of a function describes the number of arguments that the function takes. From this point on we will use both the function name and its arity to describe functions throughout the documentation. trunc/1 identifies the function which is named trunc and takes 1 argument, whereas trunc/2 identifies a different (nonexistent) function with the same name but with an arity of 2.

source: Basic types - The Elixir programming language

Assuming only world/1 was defined, you’d see an undefined function when calling it with two parameters and vice versa. You could make the params optional by specifying default params e.g. def world(conn, params \\ %{}) or def world(conn, params \\ []) and then you wouldn’t see an undefined function error when calling world/2 with one parameter.

3 Likes

Ok ! thank you :slight_smile: