liveBook · Manning

Recursion

As explained in Julia as a Second Language

Recursion is a programming technique in which a computation invokes itself on smaller or simpler inputs until a terminating condition is reached.

Recursion repeats a computation by having a function call itself. Each call needs to move toward a base case: a condition that returns a result without making another recursive call. It offers an alternative to repeating work with a `while` or `for` loop. section 3.10.1 section 3.10.2

When the stopping condition is missing

The Julia definition `fac(n) = n*fac(n-1)` looks like a way to calculate factorial. But decreasing `n` is not enough: `fac` keeps calling itself even after `n` falls below 1, eventually producing a `StackOverflowError`. An `if` statement can provide the missing exit. section 3.6

Fibonacci numbers give recursion a natural shape. To calculate a number beyond the first two, add the two preceding numbers: `fib(n-1) + fib(n-2)`. For `n` equal to 0 or 1, return `n` instead. Those first two results are the base cases that stop the calls. section 3.10 section 3.10.1

Listing 3.17 Calculating a Fibonacci number
function fib(n)
    if n == 0                  #1
        0
    elseif n == 1              #1
        1
    else
        fib(n-1) + fib(n-2)    #2
    end
end

The Julia `fib` listing puts its stopping cases before the calls that calculate smaller Fibonacci numbers. section 3.10

The same calculation can use a loop. An iterative Fibonacci function tracks earlier values and updates them on each pass, requiring more bookkeeping than the recursive version. Recursion can be simpler to write, but is often slower; if performance becomes a problem, iteration is an option. section 3.10.2

Listing 3.19 Calculating a Fibonacci number using iteration
function fibi(n)
    if 0 <= n <= 1
        return n     #1
    end
 
    prev = 0         #2
    x    = 1         #3
    for i in 2:n
        tmp = x
        x += prev    #4
        prev = tmp
    end
    x
end

Compare the iterative Fibonacci listing with the recursive one: the loop replaces self-calls with variables that track previous results. section 3.10.2

How Julia as a Second Language covers it

How deep it goes
Works through self-calls, stopping cases, a stack overflow, and an iterative comparison without detailing the call stack.
Examples
Uses a failing factorial definition and recursive and iterative Fibonacci implementations motivated by rabbit-population growth.
What it assumes
Builds on function calls and `if` statements while teaching why a recursive function needs a stopping condition.
What you can do after
Build it from scratch: write a terminating Fibonacci function and consider an iterative rewrite when recursion is too slow.
How it connects
Connects recursive functions to base cases, conditional execution, iteration, and performance.

Also explained in

Compare how 23 books explain recursion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.