<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/do-not-fear-recursion-117klxbfu" -->

---
title: Do Not Fear Recursion | daily.dev
description: Tail recursion allows recursive code to be compiled into iterative-equivalent assembly with no stack growth penalty. Using the GCD algorithm as a concrete...
canonical: https://daily.dev/posts/do-not-fear-recursion-117klxbfu
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Do Not Fear Recursion | daily.dev
og:description: Tail recursion allows recursive code to be compiled into iterative-equivalent assembly with no stack growth penalty. Using the GCD algorithm as a concrete...
og:url: https://daily.dev/posts/do-not-fear-recursion-117klxbfu
og:image: https://api.daily.dev/og/posts/117KLXBfU.png
og:image:alt: Do Not Fear Recursion
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Do Not Fear Recursion

**[Jacek Galowicz](https://daily.dev/sources/galowicz)** · 7 min read · 1 upvotes · 1 comments

## Summary

Tail recursion allows recursive code to be compiled into iterative-equivalent assembly with no stack growth penalty. Using the GCD algorithm as a concrete example, the post demonstrates that clang++ with -O2 produces identical assembly output for both recursive and iterative implementations. It explains what tail recursion is, how to identify it, how to transform non-tail-recursive functions into tail-recursive ones by accumulating state in parameters, and why this matters for writing clean, readable C++ code without sacrificing performance.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://galowicz.de/blog/dont_fear_recursion/>

## Similar posts on daily.dev

- [When compilers surprise you — Matt Godbolt’s blog](https://daily.dev/posts/when-compilers-surprise-you-matt-godbolt-s-blog-3c6va0gvq) · Hacker News · 1 upvotes · 0 comments
- [Tail-call optimization in C is relatively recent \[LWN.net\]](https://daily.dev/posts/tail-call-optimization-in-c-is-relatively-recent-lwn-net--a266wrnhm) · Hacker News · 0 upvotes · 0 comments
- [Rust should have stable tail calls](https://daily.dev/posts/rust-should-have-stable-tail-calls-6xibbzdnd) · Lobsters · 8 upvotes · 0 comments

---

Tags: [#algorithms](https://daily.dev/tags/algorithms), [#c++](https://daily.dev/tags/c++), [#recursion](https://daily.dev/tags/recursion), [#compiler-optimization](https://daily.dev/tags/compiler-optimization)

[View this post on daily.dev](https://daily.dev/posts/do-not-fear-recursion-117klxbfu)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Do Not Fear Recursion","url":"https://daily.dev/posts/do-not-fear-recursion-117klxbfu","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/do-not-fear-recursion-117klxbfu"},"datePublished":"2026-04-18T22:20:03.184Z","dateModified":"2026-04-18T22:20:27.533Z","description":"Tail recursion allows recursive code to be compiled into iterative-equivalent assembly with no stack growth penalty. Using the GCD algorithm as a concrete...","image":"https://media.daily.dev/image/upload/s--foaA6JGU--/f_auto/v1722860399/public/Placeholder%2004","thumbnailUrl":"https://media.daily.dev/image/upload/s--foaA6JGU--/f_auto/v1722860399/public/Placeholder%2004","isAccessibleForFree":true,"articleSection":"Jacek Galowicz","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Jacek Galowicz","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/da946494d89c429fbc005c1a0a1b05ba","url":"https://daily.dev/sources/galowicz"},"commentCount":1,"discussionUrl":"https://daily.dev/posts/do-not-fear-recursion-117klxbfu","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":1}],"keywords":"algorithms,c++,recursion,compiler-optimization","timeRequired":"PT7M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Jacek Galowicz","item":"https://daily.dev/sources/galowicz"},{"@type":"ListItem","position":3,"name":"Do Not Fear Recursion"}]}
```

