The goal is to explain how to write a function, for instance the following function.

In [1]:
function mandelbrot_when_explodes(parameter, length) 
    current_state = 0
    for i in 2:length
        new_state = mandelbrot_law(current_state, parameter)
        if abs(new_state) > 20
            return i
        else
            current_state = new_state
        end
    end
    return length
end
Out[1]:
mandelbrot_when_explodes (generic function with 1 method)

While the function is usually read top to bottom, this is certainly not the way it is originally written. Rather, a more convenient way to write (or explain or teach) a function is left to right (in terms of indentation) - first we write the least indented commands, then we go down in more details.

We will probably start with something like this:

In [ ]:
function mandelbrot_when_explodes(INPUT)
    return ITERATION STEP AT WHICH IT STARTS EXPLODING
end

This is not even syntactically correct, but it is a useful first step. Now we will start deleting the all-caps messages that we have written to ourselves and start translating it into computer code.

In [ ]:
function mandelbrot_when_explodes(parameter, length)
    return ITERATION STEP AT WHICH IT STARTS EXPLODING
end
In [ ]:
function mandelbrot_when_explodes(parameter, length)
    current_state = 0 # We start at z=0
    return ITERATION STEP AT WHICH IT STARTS EXPLODING
end
In [ ]:
function mandelbrot_when_explodes(parameter, length)
    current_state = 0 # We start at z=0
    for i in SOMETHING TO DO WITH MAXIMUM NUMBER OF STEPS
        CALCULATE NEW STATE AND CHECK WHETHER IT EXPLODED
    end
    return ITERATION STEP AT WHICH IT STARTS EXPLODING
end
In [ ]:
function mandelbrot_when_explodes(parameter, length)
    current_state = 0 # We start at z=0
    for i in 2:length # We skip i=1, given that the first step (i.e. i=1) is simply current_state=0
        CALCULATE NEW STATE AND CHECK WHETHER IT EXPLODED
    end
    return ITERATION STEP AT WHICH IT STARTS EXPLODING
end
In [ ]:
function mandelbrot_when_explodes(parameter, length)
    current_state = 0 # We start at z=0
    for i in 2:length # We skip i=1, given that the first step (i.e. i=1) is simply current_state=0
        new_state = mandelbrot_law(current_state, parameter)
        CHECK WHETHER IT EXPLODED
    end
    return ITERATION STEP AT WHICH IT STARTS EXPLODING
end
In [ ]:
function mandelbrot_when_explodes(parameter, length)
    current_state = 0 # We start at z=0
    for i in 2:length # We skip i=1, given that the first step (i.e. i=1) is simply current_state=0
        new_state = mandelbrot_law(current_state, parameter)
        if abs(new_state) > 20
            IF IT EXPLODED, RETURN THE STEP i AT WHICH IT EXPLODES
            OTHERWISE CONTINUE TO THE NEXT STEP, UNTIL WE RUN OUT OF PATIENCE
        end
    end
    return ITERATION STEP AT WHICH IT STARTS EXPLODING
end
In [ ]:
function mandelbrot_when_explodes(parameter, length)
    current_state = 0 # We start at z=0
    for i in 2:length # We skip i=1, given that the first step (i.e. i=1) is simply current_state=0
        new_state = mandelbrot_law(current_state, parameter)
        if abs(new_state) > 20
            IF IT EXPLODED, RETURN THE STEP i AT WHICH IT EXPLODES
        else
            OTHERWISE CONTINUE TO THE NEXT STEP, UNTIL WE RUN OUT OF PATIENCE
        end
    end
    return ITERATION STEP AT WHICH IT STARTS EXPLODING
end
In [ ]:
function mandelbrot_when_explodes(parameter, length)
    current_state = 0 # We start at z=0
    for i in 2:length # We skip i=1, given that the first step (i.e. i=1) is simply current_state=0
        new_state = mandelbrot_law(current_state, parameter)
        if abs(new_state) > 20 # Did it explode?
            return i # It exploded at step i
        else
            current_state = new_state # It did not explode, so we go to the next step
        end
    end
    return ITERATION STEP AT WHICH IT STARTS EXPLODING
end
In [ ]:
function mandelbrot_when_explodes(parameter, length)
    current_state = 0 # We start at z=0
    for i in 2:length # We skip i=1, given that the first step (i.e. i=1) is simply current_state=0
        new_state = mandelbrot_law(current_state, parameter)
        if abs(new_state) > 20 # Did it explode?
            return i # It exploded at step i
        else
            current_state = new_state # It did not explode, so we go to the next step
        end
    end
    return length # If it has not exploded in any of the tests, we just return some large number of steps, e.g. `length`
end