## DEV Community is a community of 665,832 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Recursive functions Daniel Widgren
Interested in technology and building Nova a new web framework in erlang.

This is me trying to describe recursive functions in Erlang. How you can write them and how it works.

Erlang uses recursive functions often because in other languages you have for, while and other loops. In Erlang we do it with recursive functions.

Example:

``````-module(recursive).

-export([sum/1]).

sum([]) ->
0;
``````

Here we do a sum function that will take a list of integers and summarize them.

``````15> recursive:sum([1,2,3]).
6
``````

What happen when we call our function?

Thing in Erlang works that we go from the top to the bottom of our code.

recursive:sum([1,2,3]). When we call this function with argument [1,2,3] this will happen.

``````sum([]) ->
0;
sum([1 | [2,3]]) ->
1 + sum([2,3]).
``````

First we will hit the first function clause and because our list contain three elements it will go on to the next function clause. Here the head of the list will be 1 and the tail will be list with two elements [2,3].

The recursive call that we call our function sum again but now with the tail will give us.

``````sum([]) ->
0;
sum([2 | ]) ->
2 + sum().
``````

We also do that with the last element in the list.

``````sum([]) ->
0;
sum([3 | []]) ->
3 + sum([]).
``````

Last call here will hit the base case and return a 0.

This will calculate 3 + 2 + 1 + 0 (base case) = 6.

You can also do this with tail recursive so you pass the result of the recursive to the next.

``````sum([], Sum) ->
Sum;
``````

This is our function with tail recursive. What will happen here is very similar to what we did above. Different now is that we to the addition in the recursive step and send the result to the next recursive. When we hit base case we will return the result.

``````16> c(recursive).
{ok,recursive}
17> recursive:sum([1,2,3], 0).
6
``````

We have now created a function called sum that now have aritry 2. It will take the list with integers and the start value.

``````sum([], Sum) ->
Sum;
sum([1 | [2,3]], 0) ->
sum([2,3], 0 + 1).
``````
``````sum([], Sum) ->
Sum;
sum([2 | ], 1) ->
sum(, 1 + 2).
``````
``````sum([], Sum) ->
Sum;
sum([3 | []], 3) ->
sum([], 3 + 3).
``````
``````sum([], 6) ->
6;