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)
      c = 2 

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

Have you read through

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