Function inside function is not defined?

I came across this strange behavior:

function f(k)
    if k == 1
        g(k) = "k = 1"
    elseif k == 2
        g(k) = "k = 2"
    end
    g(k)
end

f(1) # returns "k = 2"

f(2) # errors ERROR: UndefVarError: g not defined

What’s going on here? How can I get around this?

and messages below

1 Like

Wow, good find! Seems it’s a pretty old issue.