# Recursive functions

**URL:** <https://discourse.julialang.org/t/recursive-functions/123279>\
**Category:** New to Julia\
**Created:** [November 30, 2024, 6:30am UTC](https://discourse.julialang.org/t/recursive-functions/123279 "2024-11-30T06:30:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [November 30, 2024, 6:30am UTC](https://discourse.julialang.org/t/recursive-functions/123279/1 "2024-11-30T06:30:02Z")

</div>

Hello,  
Consider the following recursive function:

```julia
function decToBin(n)
    if n > 0
        decToBin(n ÷ 2)
        print("$(n % 2)")
    end
end

```

For example,` n=13`. The `n` is greater than `0` and the line `decToBin(n ÷ 2)` is executed. Does the function get called again and the statement `print("$(n % 2)")` is not executed?

Thank you.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 30, 2024, 7:11am UTC](https://discourse.julialang.org/t/recursive-functions/123279/2 "2024-11-30T07:11:56Z")

</div>

> [@hack3rcon](#):
>
> Does the function get called again and the statement `print("$(n % 2)")` is not executed?

Correct, that’s how recursion works

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [November 30, 2024, 7:46am UTC](https://discourse.julialang.org/t/recursive-functions/123279/3 "2024-11-30T07:46:05Z")

</div>

> [@hack3rcon](#):
>
> and the statement `print("$(n % 2)")` is not executed?

I might have misunderstood the question, but I don’t think that’s correct.

The print is of course executed for each call to `decToBin` where `n` is `> 0`, it just happens after the call to `decToBin` finishes. So for `n = 13` the function will first call itself (\*) and then `print` will be called with `13 % 2`.

(\*) Of course when it calls itself with `13 ÷ 2 = 6`, it will first call itself (\*\*) and then `print` will be called with `6 % 2`.

(\*\*) Of course when it calls itself with `6 ÷ 2 = 3`, it will first call itself (\*\*\*) and then `print` will be called with `3 % 2`.

(\*\*\*) …

The lowest “level” of this recursion is when the first `print` will happen, then the one in the level above, then the one above etc. until (\*) finishes and the original function can continue with the original print statement `print("$(13 % 2)")` which will appear last on the screen (although the function is called first).

Or in another visual way:

```julia
function decToBin(n)
    if n > 0
        decToBin(n ÷ 2) -----> if (n ÷ 2) > 0
        print("$(n % 2)") decToBin((n ÷ 2) ÷ 2) -----> if ((n ÷ 2) ÷ 2) > 0
    end print("$((n ÷ 2) % 2)") ...
    (return nothing) end

```

(I added the implicit `return nothing` here to illustrate that the return value of `print` would be returned, which is `nothing` here).

This would be very different to (note the extra `return` statement which prevents any `print` from being executed):

```julia
function decToBin(n)
    if n > 0
        return decToBin(n ÷ 2)
        print("$(n % 2)")
    end
end

```

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [November 30, 2024, 9:19am UTC](https://discourse.julialang.org/t/recursive-functions/123279/4 "2024-11-30T09:19:31Z")

</div>

Hi,  
Thank you so much for your great answer.
