# Problem variable declaration

**URL:** https://discourse.julialang.org/t/problem-variable-declaration/73617
**Category:** New to Julia
**Tags:** scope
**Created:** [December 25, 2021, 3:42pm UTC](https://discourse.julialang.org/t/problem-variable-declaration/73617 "2021-12-25T15:42:45Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![kaushik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kaushik/32/32801_2.png) [@kaushik](https://discourse.julialang.org/u/kaushik)
#### Post date: [December 25, 2021, 3:42pm UTC](https://discourse.julialang.org/t/problem-variable-declaration/73617/1 "2021-12-25T15:42:45Z")

</div>

I have written a simple code of gradient descent. It runs well in Jupyter, but when I try to execute it on VS Code or run from Julia REPL, it shows an error that  
“LoadError: UndefVarError: x\_old not defined”

The code is as below:

```julia
maxitr=100
itr=1
x_old=10
L=0.1
x_new=[]
while itr<maxitr
    x_new=x_old-L*2(x_old-4)
    if abs(x_new-x_old)>10^-10
        x_old=x_new
        itr=itr+1
    end
end
print(x_new)

```

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [December 25, 2021, 4:16pm UTC](https://discourse.julialang.org/t/problem-variable-declaration/73617/2 "2021-12-25T16:16:18Z")

</div>

You are running quite an old Julia version 1.0 , 1.4?  
You should update to the current release.

But what you see is the infamous rule of scope:  
In general: [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1.0/manual/variables-and-scoping/)

For your Julia 1.0 you can put all your code into a function to solve it:

```julia
julia> function myFunc()
       maxitr=100
       itr=1
       x_old=10
       L=0.1
       x_new=[]
       while itr<maxitr
           x_new=x_old-L*2(x_old-4)
           if abs(x_new-x_old)>10^-10
               x_old=x_new
               itr=itr+1
           end
       end
       x_new
       end

julia> print( myFunc() )
4.000000001527777

```

or you can use a let block:

```julia
let x_old=10, itr=1, x_new=[], maxitr=100, L=0.1
	while itr<maxitr
		x_new=x_old-L*2(x_old-4)
		if abs(x_new-x_old)>10^-10
			x_old=x_new
			itr=itr+1
		end
	end
	print(x_new)
end

```

There are other solutions like using `global`, but the best is to upgrade to the current release.

---

<div class="post-metadata">

### Author: ![kaushik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kaushik/32/32801_2.png) [@kaushik](https://discourse.julialang.org/u/kaushik)
#### Post date: [December 25, 2021, 4:31pm UTC](https://discourse.julialang.org/t/problem-variable-declaration/73617/3 "2021-12-25T16:31:29Z")

</div>

Thanks, Oheil for your suggestions, but I am running 1.7.0. SO did you mean to use function always?

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [December 25, 2021, 5:30pm UTC](https://discourse.julialang.org/t/problem-variable-declaration/73617/4 "2021-12-25T17:30:04Z")

</div>

I don’t get the error since version 1.5 and I tried 1.5, 1.6 and 1.7.0 and 1.7.1 now in a plain REPL and in 1.6.1 in a VSCode REPL.

Using functions is in general a recommended thing in Julia.

Do you have something about scope activated like e.g. [https://github.com/stevengj/SoftGlobalScope.jl](https://github.com/stevengj/SoftGlobalScope.jl) from your `startup.jl` files. This file is currently located in `~/.julia/config/` .

Perhaps the output of `versioninfo()` and `] st` helps to understand why you get the error in 1.7.1 and I do not.

---

<div class="post-metadata">

### Author: ![kaushik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kaushik/32/32801_2.png) [@kaushik](https://discourse.julialang.org/u/kaushik)
#### Post date: [December 26, 2021, 6:07pm UTC](https://discourse.julialang.org/t/problem-variable-declaration/73617/5 "2021-12-26T18:07:23Z")

</div>

Thanks Oheil for the documents. I have gone through this. The global/local scope should not there in 1.7.1. Although I installed the package "SoftGlobalScope:, but still I have the same output. I have attached one screenshot of my terminal.

 ![ss](https://global.discourse-cdn.com/julialang/original/3X/5/d/5d029c0b0a54cec5f3b29a1912a217804622327f.png)

Next is the startup.jl file. I am using MAC and in my .julia folder, there is no config file.

Extremely sorry to bother you, but I desperately need the solution. Please let me know if you need to know anything else.

Many thanks for your time.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [December 26, 2021, 6:21pm UTC](https://discourse.julialang.org/t/problem-variable-declaration/73617/6 "2021-12-26T18:21:05Z")

</div>

Ah, I see. I thought you are running your code in the REPL as you said in the OP.  
FYI the REPL is the interactive command line which waits for your input when you start julia without a .jl file.  
You can solve your issue the easiest by putting

```julia
let
...
end

```

around everything.

---

<div class="post-metadata">

### Author: ![kaushik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kaushik/32/32801_2.png) [@kaushik](https://discourse.julialang.org/u/kaushik)
#### Post date: [December 26, 2021, 6:43pm UTC](https://discourse.julialang.org/t/problem-variable-declaration/73617/7 "2021-12-26T18:43:30Z")

</div>

Thanks a lot. It works.
