# How is defer timed with recursive calls?

**URL:** <https://forums.swift.org/t/how-is-defer-timed-with-recursive-calls/20552>\
**Category:** Using Swift\
**Tags:** recursion\
**Created:** [February 16, 2019, 1:41am UTC](https://forums.swift.org/t/how-is-defer-timed-with-recursive-calls/20552 "2019-02-16T01:41:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![CTMacUser](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ctmacuser/32/1802_2.png) [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Post date:** [February 16, 2019, 1:41am UTC](https://forums.swift.org/t/how-is-defer-timed-with-recursive-calls/20552/1 "2019-02-16T01:41:30Z")

</div>

My code for a [loop-less sequence-on-filter-sequence method](https://forums.swift.org/t/how-to-effectively-filter-array-by-another-array-in-functional-paradigm/20442/9) required me to add a `do`-block to prevent infinite recursion. It seems that if a return path involves a (tail?) recursive call, then outstanding `defer` blocks don't trigger until the recursive call finishes. Since my code needed the increment(s) to happen before the recursion, I added the `do` structuring.

Am I understanding it correctly?

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [February 16, 2019, 2:00am UTC](https://forums.swift.org/t/how-is-defer-timed-with-recursive-calls/20552/2 "2019-02-16T02:00:45Z")

</div>

That sounds correct. `defer` happens "at the `return` (or `break` or `continue`)" or "at the close brace", and that means after any recursive calls to compute the value to return.
