# Tail-call recursion

**URL:** <https://discourse.julialang.org/t/tail-call-recursion/87847>\
**Category:** Performance\
**Created:** [September 26, 2022, 10:09pm UTC](https://discourse.julialang.org/t/tail-call-recursion/87847 "2022-09-26T22:09:53Z")\
**Posts on this page:** 2\
**Page:** 3

<div class="post-metadata">

**Author:** ![lzhu](https://avatars.discourse-cdn.com/v4/letter/l/ac91a4/32.png) [@lzhu](https://discourse.julialang.org/u/lzhu)\
**Post date:** [December 14, 2022, 9:34pm UTC](https://discourse.julialang.org/t/tail-call-recursion/87847/41 "2022-12-14T21:34:29Z")

</div>

Error problem can be handled by adding a specialized TCO error message, possibly with TCO only triggering on a TCO annotation.

The bigger argument for TCO vs loops is that while TCO always can translate to a loop, the TCO recursive function is easier to read (for people comfortable with recursion) and shorter.

Consider:

fac(n, acc = 1) = 1 == n ? acc : fac (n-1, n\*acc)

vs

function fac(n)  
acc = 1  
for elem in 1:n  
acc \*= elem  
end  
return acc  
end

with TCO, the former has no performance penalty over the latter, and is terser. This means your code actually ends up being shorter than Python, which can be a selling point (and is an essential advantage of functional style).

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 15, 2022, 7:38pm UTC](https://discourse.julialang.org/t/tail-call-recursion/87847/42 "2022-12-15T19:38:26Z")

</div>

Or you just use reductions: `prod(1:n)`.

[Previous page](https://discourse.julialang.org/t/tail-call-recursion/87847.md?page=2)
