Strange scope issue

In this code, the variable inside the loop “c” is not referenced before it’s value is assigned at runtime, but julia will throw an error
ERROR: UndefVarError: c not defined

  for n_iter in 0:10
      if n_iter != 0
          println(n_iter, " ", c)
      end
      c = 2 
  end

To me this is a bit surprising (wouldn’t happen in python). What is the suggested workaround?

Have you read through https://docs.julialang.org/en/stable/manual/variables-and-scoping/#scope-of-variables-1?

1 Like

It’s not a workaround since this is not a bug, but you should either assign a value to c before the for loop or declare it with local c.

1 Like