# How to define a numerical value in Julia?

**URL:** <https://discourse.julialang.org/t/how-to-define-a-numerical-value-in-julia/69574>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 11, 2021, 5:23pm UTC](https://discourse.julialang.org/t/how-to-define-a-numerical-value-in-julia/69574 "2021-10-11T17:23:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 11, 2021, 5:23pm UTC](https://discourse.julialang.org/t/how-to-define-a-numerical-value-in-julia/69574/1 "2021-10-11T17:23:32Z")

</div>

I keep getting the below error:  
`LoadError: UndefVarError: n1 not defined`

for my below code:

```julia
n1 = 0;
for i in 1:m[1], j in 1:m[2]
        n1 += 1;
        A[n1,:] = [X0[i,j] Y0[i,j] E0[i,j]];
end

```

Hasn’t my “n1=0;” defined n1? Anyone has any clue? Thanks!

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 11, 2021, 5:28pm UTC](https://discourse.julialang.org/t/how-to-define-a-numerical-value-in-julia/69574/2 "2021-10-11T17:28:17Z")

</div>

It is a scoping problem. Read this: [Scope of loops · JuliaNotes.jl](https://m3g.github.io/JuliaNotes.jl/stable/loopscopes/)

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 11, 2021, 5:45pm UTC](https://discourse.julialang.org/t/how-to-define-a-numerical-value-in-julia/69574/3 "2021-10-11T17:45:56Z")

</div>

Many thanks. So what is the best fix? The below? Coming from Matlab, I think this is so weird!

```julia
n1 = 0;
for i in 1:m[1], j in 1:m[2]
        global n1;
        n1 += 1;
        A[n1,:] = [X0[i,j] Y0[i,j] E0[i,j]];
end

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 11, 2021, 5:49pm UTC](https://discourse.julialang.org/t/how-to-define-a-numerical-value-in-julia/69574/4 "2021-10-11T17:49:40Z")

</div>

The best solution is to write everything into functions:

```julia
julia> function f()
           n1 = 0
           for i in 1:10
               n1 += 1
           end
           n1
       end
f (generic function with 2 methods)

julia> f()
10

```

Then pass `A`, `m`, `X0`, `Y0`, `E0`, as parameters to that function. That not only avoids the scoping problem, but is fundamental for the performance of the code. See: [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/)

(not using global variables is the first tip and the most fundamental rule that is needed to start getting Julia code to run fast)

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 11, 2021, 5:58pm UTC](https://discourse.julialang.org/t/how-to-define-a-numerical-value-in-julia/69574/5 "2021-10-11T17:58:55Z")

</div>

ps: there are other Julia anti-patterns in that code. If you provide an actual running example, with the expected output, we can give you better advice in general.

For example:

```julia
A[n1,:] = [X0[i,j] Y0[i,j] E0[i,j]];

```

This is allocating a new `(1,3)` matrix on the right, which can be improved just by using a tuple `()` instead of a vector `[]`, but also you are running over the last dimension first, which is slower, because Julia is column-major. I’m not sure if performance is important in this specific case, but on the long run it is good to get the feeling of these ideas.

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [October 11, 2021, 6:47pm UTC](https://discourse.julialang.org/t/how-to-define-a-numerical-value-in-julia/69574/6 "2021-10-11T18:47:46Z")

</div>

> [@lmiq](#):
>
> because Julia is column-major

Maybe pertinent to note that this comment also applies to matlab code.
