# X not defined error, even though it is?

**URL:** https://discourse.julialang.org/t/x-not-defined-error-even-though-it-is/20714
**Category:** General Usage
**Created:** [February 12, 2019, 5:34pm UTC](https://discourse.julialang.org/t/x-not-defined-error-even-though-it-is/20714 "2019-02-12T17:34:06Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)
#### Post date: [February 12, 2019, 5:34pm UTC](https://discourse.julialang.org/t/x-not-defined-error-even-though-it-is/20714/1 "2019-02-12T17:34:06Z")

</div>

Hi,

I have this code:

```julia
using Pkg;

n = 1.0;

function yfc(t)
    # x in here is actually t from before
    cos(1/3 * t^3 + n*t)
end;

const t0 = 0;
const tf = 1000;
const N = 10000000;
const dt = 1000 / N;
t = t0:dt:tf;
const x = 0.0 ;

for i in 1:N;
    k1 = dt * yfc(t[i]);
    k2 = dt * yfc(t[i] + 1/2*dt);
    k4 = dt * yfc(t[i+1]);
    x = x + 1.0/6.0 * k1 + 2.0/3.0 * k2 + 1.0/6.0 * k4 ;
end

```

and I’m getting the error:

```bash
ERROR: LoadError: UndefVarError: x not defined
Stacktrace:
 [1] top-level scope at /data/GitHub/mine/math/julia-scripts/airy-ai-approx.jl:24 [inlined]
 [2] top-level scope at ./none:0
 [3] include(::String) at ./client.jl:403
 [4] top-level scope at none:0
in expression starting at /data/GitHub/mine/math/julia-scripts/airy-ai-approx.jl:20

```

which confuses me. x is defined, originally as a constant and then as a variable that is updated over the loop. What is the problem here?

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [February 12, 2019, 5:37pm UTC](https://discourse.julialang.org/t/x-not-defined-error-even-though-it-is/20714/2 "2019-02-12T17:37:17Z")

</div>

I believe that the [notorious global scope issue](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894) is at play here, but, more importantly, `const` means constant, so if you declare something `const`, you should not attempt to assign it.

---

<div class="post-metadata">

### Author: ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)
#### Post date: [February 12, 2019, 5:50pm UTC](https://discourse.julialang.org/t/x-not-defined-error-even-though-it-is/20714/3 "2019-02-12T17:50:48Z")

</div>

Yep, but if I remove the `const` before `x` I get the same error. Adding const was my attempt to fix this original issue.

---

<div class="post-metadata">

### Author: ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)
#### Post date: [February 12, 2019, 5:54pm UTC](https://discourse.julialang.org/t/x-not-defined-error-even-though-it-is/20714/4 "2019-02-12T17:54:59Z")

</div>

Looking at the linked post I thought maybe adding an if conditional for i=1 would fix it, like changing the for loop to:

```bash
for i in 1:N;
    k1 = dt * yfc(t[i]);
    k2 = dt * yfc(t[i] + 1/2*dt);
    k4 = dt * yfc(t[i+1]);
    if i == 1
        x = 0;
    end
    x = x + 1.0/6.0 * k1 + 2.0/3.0 * k2 + 1.0/6.0 * k4 ;
end

```

and removing the above (before the loop) `x = 0.0;` declaration would fix it, but nope same x undefined error.

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [February 12, 2019, 6:21pm UTC](https://discourse.julialang.org/t/x-not-defined-error-even-though-it-is/20714/5 "2019-02-12T18:21:38Z")

</div>

Again, this is the global scope issue, see my link above. I think the summary is that this behavior will change in a future version of Julia, as you can see it has been discussed ad nauseum.

In the meantime, this really isn’t something you would want in global scope anyway. You should put such code in functions, and if the functions ever do need to reference global variables, those should be declared `const`.

---

<div class="post-metadata">

### Author: ![fnin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fnin/32/23300_2.png) [@fnin](https://discourse.julialang.org/u/fnin)
#### Post date: [February 12, 2019, 6:33pm UTC](https://discourse.julialang.org/t/x-not-defined-error-even-though-it-is/20714/6 "2019-02-12T18:33:12Z")

</div>

x is found to be undefined, because you are trying to use it in a for loop (local scope) inside the global scope. This local scopes don’t (with some exceptions) inherit variables from the global scope unless it is explicitly stated with the global keyword.

For your understanding I advise you to read the [documentations](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#struct)  
part about scope of variables.

Try this:

```julia
n = 1.0

function yfc(t)
    # x in here is actually t from before
    cos(1/3 * t^3 + n*t)
end

const t0 = 0
const tf = 1000
const N = 10000000
const dt = 1000 / N
t = t0:dt:tf
x = 0.0 

for i in 1:N
    global x
    k1 = dt * yfc(t[i])
    k2 = dt * yfc(t[i] + 1/2*dt)
    k4 = dt * yfc(t[i+1])
    x = x + 1.0/6.0 * k1 + 2.0/3.0 * k2 + 1.0/6.0 * k4 
end

julia> x
0.6663639957011587

```

But you should really think about putting the code in a function if you want it to run perfomantly.

PS: There is no need to put a semicolon behind every statement. This is only useful to suppress the output of things entered in the REPL.
