# Recursion Recursion Is when a function calls itself. Recursion often leads to code that is easy to comprehend and to establish the correctness of, because it often directly corresponds to the way we think. Take the [[Factorial]] of an integer for instance, which is defined as a product off all of the integer's preceding positive integers (and itself). This exactly corresponds to the definition of the [[Factorial]] found in math textbooks, making it easy to establish the code's correctness. ```fsharp //Note: this fails for negative inputs and is not tail recursive let rec f n = if n = 0 then 1 else n * f (n - 1) ``` ## Tail Recursion The code above will unfortunately quickly lead to a stack overflow exception because a stack frame is pushed for each integer. This can easily be mitigated by converting the code to a tail recursive one if the language compiler supports [[Tail-Call Optimization]], but as many object-oriented programming languages do not, many programmers shun recursion completely. Luckily, [[Tail-Call Optimization]] can easily be implemented manually (see [[Trampoline]]), and non tail recursive functions can easily be converted into tail recursive ones.