Is it necessary to use "global" with try/catch inside a function?

I’m running a simple find_zero(), and to help me identify errors the core of the function being solved is inside a try/catch circuit:

function getA(input)

       # then main simulation
       println("Running 1982 onwards ")
       try
              results = S82to20(s82, input, state1981, k81);
       catch err
              print(err)
              println("with input ", input)
              throw(error())
       end
       
       cap = results[4];

       return f(cap)
end

solution = find_zero(getA, iniInput)

However, I get the error “results not found”. I understand this is a scoping problem, and the easiest fix is to prefix “global” inside the try/catch. But I only want “results” to fall within function scope, not universal scope. So my questions are:

  • If I prefix “global”, does that place results in function scope or top-level scope?
  • If the former, how would I (hypothetically) place it in top-level scope instead?
  • If the latter, is there an alternative to “global” that places it in function scope?

Initializing results is enough, no need for global:

function getA(input)
  results=0  #or whatever suits
  try ...
2 Likes

Or just local results:

function getA(input)
  local results
  try
    ...

or

function getA(input)
 results = try
    ...
5 Likes