# No error when using () for indexing inside a loop

**URL:** <https://discourse.julialang.org/t/no-error-when-using-for-indexing-inside-a-loop/19970>\
**Category:** General Usage\
**Created:** [January 23, 2019, 10:17am UTC](https://discourse.julialang.org/t/no-error-when-using-for-indexing-inside-a-loop/19970 "2019-01-23T10:17:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![v-i-s-h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v-i-s-h/32/46152_2.png) [@v-i-s-h](https://discourse.julialang.org/u/v-i-s-h)\
**Post date:** [January 23, 2019, 10:17am UTC](https://discourse.julialang.org/t/no-error-when-using-for-indexing-inside-a-loop/19970/1 "2019-01-23T10:17:40Z")

</div>

I accidentally came across this observation, when trying to create a symmetric matrix.

```julia
julia> versioninfo()
Julia Version 1.1.0
Commit 80516ca202 (2019-01-21 21:24 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)

julia> z = randn(5,5)
5×5 Array{Float64,2}:
  1.35901 -0.507248 1.60027 1.53572 -1.51329 
 -0.0657509 -0.91781 0.0559653 -0.133623 1.06091 
  0.434098 1.0356 -0.545556 0.3141 0.595068
  1.17286 0.88539 0.175035 0.258012 -1.66506 
  1.32398 -1.16009 0.238903 -0.468032 0.70049 

julia> for i = 1:size(z,1)
         for j = 1:i
           z(i,j) = z(j,i) # Shouldn't it have raised an error here?
         end
       end

julia> z
5×5 Array{Float64,2}:
  1.35901 -0.507248 1.60027 1.53572 -1.51329 
 -0.0657509 -0.91781 0.0559653 -0.133623 1.06091 
  0.434098 1.0356 -0.545556 0.3141 0.595068
  1.17286 0.88539 0.175035 0.258012 -1.66506 
  1.32398 -1.16009 0.238903 -0.468032 0.70049 

```

But if I do without loop, it throws error

```julia
julia> i = 2; j = 3;

julia> z(i,j)
ERROR: MethodError: objects of type Array{Float64,2} are not callable
Use square brackets [] for indexing an Array.
Stacktrace:
 [1] top-level scope at none:0

```

Is this expected behavior?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [January 23, 2019, 10:23am UTC](https://discourse.julialang.org/t/no-error-when-using-for-indexing-inside-a-loop/19970/2 "2019-01-23T10:23:10Z")

</div>

> [@v-i-s-h](#):
>
> ```julia
> z(i,j) = z(j,i) # Shouldn't it have raised an error here?
> 
> ```

No, this is creating a new local function called `z`.

---

<div class="post-metadata">

**Author:** ![v-i-s-h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v-i-s-h/32/46152_2.png) [@v-i-s-h](https://discourse.julialang.org/u/v-i-s-h)\
**Post date:** [January 23, 2019, 10:38am UTC](https://discourse.julialang.org/t/no-error-when-using-for-indexing-inside-a-loop/19970/3 "2019-01-23T10:38:19Z")

</div>

Thanks.  
That explains it. 🙂
