# UndefVarError for some variables

**URL:** https://discourse.julialang.org/t/undefvarerror-for-some-variables/25660
**Category:** New to Julia
**Created:** [June 25, 2019, 6:34pm UTC](https://discourse.julialang.org/t/undefvarerror-for-some-variables/25660 "2019-06-25T18:34:19Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![brunaw](https://avatars.discourse-cdn.com/v4/letter/b/f07891/32.png) [@brunaw](https://discourse.julialang.org/u/brunaw)
#### Post date: [June 25, 2019, 6:34pm UTC](https://discourse.julialang.org/t/undefvarerror-for-some-variables/25660/1 "2019-06-25T18:34:19Z")

</div>

Hey all. I’ve been trying to write some functions in Julia, but I’ve been getting many errors of the  
`UndefVarError: variable not defined` type. The main source of the errors is this part here (which is inside my function):

```julia
            global pi = rand(C)
            global pi = pi/sum(pi)
            global mu = reshape(randn(C * d), (C, d))
            sigma = zeros(Float64, (d, d, C))
            for c in 1:C
                sigma[:, :, c] = Diagonal(ones(d))
            end
            global lloss = -Inf
            
            not_saturated = true
            for i in 1:max_iter
                former_loss = copy(lloss)
                gamma = E_step(X, pi, mu, sigma)
                pi, mu, sigma = M_step(X, gamma)
               lloss = compute_vlb(X, pi, mu, sigma, gamma)
                if(former_loss[1] > lloss[1])
                   print("bug")
                end
                not_saturated = abs((lloss[1]/former_loss[1]) - 1) > rtol
            end    

```

where I get that my pi, mu, and sigma are not defined (I even tried making it global but no success so far). I know this is not reproducible (sorry for that), so I’ll try to explain. The E\_step, M\_step and compute\_vlb functions will receive the parameters inside the `for` loop there, and what I expect is that the parameters can be updated at each iteration (as is written there). I’m imagining that there’s something very simple that I’m missing here. Does anyone have any idea?

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [June 25, 2019, 7:11pm UTC](https://discourse.julialang.org/t/undefvarerror-for-some-variables/25660/2 "2019-06-25T19:11:13Z")

</div>

It’s really hard to say anything helpful without being able to run the code. Here are some general steps you can take to figure out what’s going on:

1. Inside of a function, you should almost never need to use the `global` keyword. The only time you would need it is if you want your function to _mutate_ a global variable, but that’s (A) bad programming practice and (B) very slow in Julia. So just don’t do it. So, as a first step, remove the `global` keyword from inside your function entirely and don’t assume your function will mutate any global variables.
2. If you still have errors, practice making a _minimal_ reproducible example. Make a copy of your code and delete _everything_ that is not directly involved in the problem you’re having. Keep deleting until either (A) you understand the problem or (B) you have something simple enough to past here as a complete example. That will make it much easier for us to help you.

---

<div class="post-metadata">

### Author: ![brunaw](https://avatars.discourse-cdn.com/v4/letter/b/f07891/32.png) [@brunaw](https://discourse.julialang.org/u/brunaw)
#### Post date: [June 25, 2019, 7:41pm UTC](https://discourse.julialang.org/t/undefvarerror-for-some-variables/25660/3 "2019-06-25T19:41:08Z")

</div>

Yes, I’m aware of that @rdeits, thanks, it’s just that the code is big (and messy at the moment), so if that was something simple maybe someone could just spot it by looking. The code is here, actually: [https://github.com/brunaw/jubs/blob/master/code/ml/em.jl](https://github.com/brunaw/jubs/blob/master/code/ml/em.jl) (it’s not so simple or organised but it runs quickly) and the data is in the same folder inside the repository.

The function that creates the issues is the last one, because of undefined variable errors. What I’m doing is trying to build it from inside out, so when I run what’s inside, I get the errors 😕

---

<div class="post-metadata">

### Author: ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)
#### Post date: [June 25, 2019, 9:09pm UTC](https://discourse.julialang.org/t/undefvarerror-for-some-variables/25660/4 "2019-06-25T21:09:43Z")

</div>

Welcome to the Julia’s discourse forum!  
I was trying your function and i find some problems in the function` compute_vlb(X, pi, mu, sigma, gamma, C = 3)`, there were a lots of variable\_var, but those variables weren’t defined. i supposed that those variables are the function imputs so i changed those names., can you try that?.

```julia
function compute_vlb(X, _pi, mu, sigma, gamma, C = 3)
    N = size(X[1])[1]
    #C = size(pi)[1]

    d = size(mu)[2]
    loss = zeros(Float64, 1)
    for c in 1:C
        for n in 1:N
            mv_norm = MvNormal(mu[c, :], Symmetric(sigma[:, :, c]))
            logd = logpdf(mv_norm, [X[1][n], X[2][n]])
            gamma_c = gamma[n, c]
            loss_here = gamma_c * ((log(_pi[c]) + logd) - log(gamma_c))
            loss[1] = loss[1] + loss_here[1]
        end
    end
return loss
end

```

also, pi is the value 3.1415…, and you where replacing it with a vector, maybe changing the variable can help (Pi, i used \_pi hahaah)

---

<div class="post-metadata">

### Author: ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)
#### Post date: [June 25, 2019, 10:49pm UTC](https://discourse.julialang.org/t/undefvarerror-for-some-variables/25660/5 "2019-06-25T22:49:53Z")

</div>

Remove the globals and work in jupyter. If you are having issues there, then it is more fundamental than a global variable issue
