# Different behaviour of the identical code

**URL:** <https://discourse.julialang.org/t/different-behaviour-of-the-identical-code/109910>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [February 8, 2024, 7:37am UTC](https://discourse.julialang.org/t/different-behaviour-of-the-identical-code/109910 "2024-02-08T07:37:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ttx002000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ttx002000/32/208489_2.png) [@ttx002000](https://discourse.julialang.org/u/ttx002000)\
**Post date:** [February 8, 2024, 7:37am UTC](https://discourse.julialang.org/t/different-behaviour-of-the-identical-code/109910/1 "2024-02-08T07:37:16Z")

</div>

Dear all, I am using VS code to compile my Julia code. I have the following code, which is “test.jl”. I also have another file that hosts my\_function, which is called “test\_test.jl”. Basically, my\_function takes into k and outputs 3\*k

```julia
using Distributed
@everywhere include("test_test.jl")
addprocs(5)
wok=workers()
an2=Vector{Float64}(undef,5)
 
 @everywhere k=6
for ja=1:5
 local ans1= @spawnat wok[ja] my_function(k)
 an2[ja]=fetch(ans1)
end

rmprocs(wok)

```

So when I execute “test.jl”, sometimes it works fine, it gives a five-element vector of 18. But sometimes the REPL says “my\_function” is not defined on workers. So when it goes wrong, the error message is  
“On worker 12:  
UndefVarError: `#my_function` not defined”. But when it is right, I can just type in an2 in REPL, and it is a 5-element vector of 18. And by “executing”, I mean clicking the top right triangle in the VScode interface, which is “Execute active file in REPL”. My “test\_test.jl” file is the following

```julia
function my_function(k)
    loop_variable=0
    for i in 1:3
        loop_variable += k
        println("Inside loop: $loop_variable")
    end
   return loop_variable
end

```

---

<div class="post-metadata">

**Author:** ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)\
**Post date:** [February 8, 2024, 7:52am UTC](https://discourse.julialang.org/t/different-behaviour-of-the-identical-code/109910/2 "2024-02-08T07:52:45Z")

</div>

You should use `addprocs` before including your file with the function, so that the processes spawn before you tell them to include ‘test\_test.jl’.

Also, consider using `pmap` instead for this kind of task:

```julia
using Distributed
addprocs(5)

@everywhere include("test_test.jl")

k=6
an2=pmap(my_function, ones(5).*k)
println(an2)
rmprocs(workers())

```
