# Uncertain about function definition order

Hello!

I am relatively new to Elixir, so far I love it!

I have a little question about pattern matching and function definition order.

I tried to implement a simple factorial function but my editor (vs code with ElixirLS extension) keeps complaining. I tried every order with little success.

function is:

`````` @spec factorial(non_neg_integer, non_neg_integer) ::
non_neg_integer | {:error, :invalid_argument}
def factorial(number, result \\ 1) when is_integer(number) and number > 0,
do: factorial(number - 1, result * number)

def factorial(0, result), do: result
def factorial(invalid_argument, _), do: {:error, :invalid_argument, invalid_argument}
``````

error is:

``````def factorial/2 has multiple clauses and also declares default values. In such cases, the default values should be defined in a header.
``````

Hello and welcome,

This happens because You have result as optional. You can solve this by adding a signature, like this…

``````def factorial(number, result \\ 1)
def factorial(number, result) when is_integer(number) and number > 0,
do: factorial(number - 1, result * number)

# etc
``````

and then use result as non optional in the rest.

2 Likes

Ho great I understand, thanks a lot @kokolegorille !