# Recursion and while loops: pros and cons

**URL:** <https://discourse.julialang.org/t/recursion-and-while-loops-pros-and-cons/106559>\
**Category:** General Usage\
**Tags:** loops, recursion\
**Created:** [November 22, 2023, 7:14am UTC](https://discourse.julialang.org/t/recursion-and-while-loops-pros-and-cons/106559 "2023-11-22T07:14:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [November 22, 2023, 7:14am UTC](https://discourse.julialang.org/t/recursion-and-while-loops-pros-and-cons/106559/1 "2023-11-22T07:14:17Z")

</div>

This recursive function:

```julia
function foo1(x)
    y = someoperations(y)
    if somecondition(y)
        return y
    else
        return foo1(y)
    end
end

```

does essentially the same as with the loop:

```julia
function foo2(x)
    y = someoperations(y)
    while !somecondition(y)
        y = someoperations(y)
    end
    return y
end

```

But Julia does not implement it in the same way. There is a function-call overhead in the recursive version - or at least there was such an overhead in earlier versions, [according to this old thread](https://discourse.julialang.org/t/recursive-call-vs-while-loop/7723/18). On the other hand, if `somecondition` is never met, the loop will run forever, whereas after a given number of recursive calls Julia will stop and throw an error, so it feels safer.

What are other advantages and drawbacks of either approach? (If there is anything to add to what was already mentioned in the linked thread above.)

---

<div class="post-metadata">

**Author:** ![HanD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hand/32/213908_2.png) [@HanD](https://discourse.julialang.org/u/HanD)\
**Post date:** [November 22, 2023, 7:56am UTC](https://discourse.julialang.org/t/recursion-and-while-loops-pros-and-cons/106559/2 "2023-11-22T07:56:53Z")

</div>

What you are looking for is _tail call elimination_ (also called as tail call optimization or tail recursion optimization). There have been a number of threads on the subject here, e.g. [this one](https://discourse.julialang.org/t/tail-call-recursion/87847). According to this discussion, “Julia doesn’t have tco currently, and is fairly unlikely to add it.” That being said, a macro is being considered [on Github](https://github.com/JuliaLang/julia/issues/4964#issuecomment-961932928).

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [November 22, 2023, 8:18am UTC](https://discourse.julialang.org/t/recursion-and-while-loops-pros-and-cons/106559/3 "2023-11-22T08:18:16Z")

</div>

This doesn’t answer your pros and cons question, but you can avoid an infinite loop by adding a variable that you increment with each iteration and check that it hasn’t exceeded some limit.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [November 22, 2023, 8:57am UTC](https://discourse.julialang.org/t/recursion-and-while-loops-pros-and-cons/106559/4 "2023-11-22T08:57:04Z")

</div>

I mark this as solution because, although I asked it as “pros and cons” of recursion vs. while-loop, my reason for asking about that is exactly what you are describing; just that I didn’t know it.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [November 22, 2023, 9:22am UTC](https://discourse.julialang.org/t/recursion-and-while-loops-pros-and-cons/106559/5 "2023-11-22T09:22:46Z")

</div>

An additional resource that I found very to understand the relationship between what I asked and what @HanD answered (with Julia examples!):

> **[Tail call | Relation to the while statement](https://en.wikipedia.org/wiki/Tail_call#Relation_to_the_while_statement)**
>
> Tail recursion can be related to the while statement, an explicit iteration, for instance by transforming
> into
> where x may be a tuple involving more than one variable: if so, care must be taken in implementing the assignment statement x ← baz(x) so that dependencies are respected. One may need to introduce auxiliary variables or use a swap construct.
> More generally,
