Help to set properly the scope of a dataframe: want it to be local to a function but not local within each interation of a for-loop inside that function

You want the reserved syntax local

julia> function foo()
           for i in 1:5
               if i == 1
                   x = 1
               else
                   x = x + i
               end
               println(x)
           end
       end
foo (generic function with 1 method)

julia> foo()
1
ERROR: UndefVarError: x not defined
Stacktrace:
 [1] foo()
   @ Main ./REPL[6]:6
 [2] top-level scope
   @ REPL[7]:1

julia> function foo()
           local x
           for i in 1:5
               if i == 1
                   x = 1
               else
                   x = x + i
               end
               println(x)
           end
       end
foo (generic function with 1 method)

julia> foo()
1
3
6
10
15
2 Likes