# Pluto - UndefVarError

**URL:** <https://discourse.julialang.org/t/pluto-undefvarerror/51480>\
**Category:** New to Julia\
**Tags:** pluto\
**Created:** [December 8, 2020, 9:29pm UTC](https://discourse.julialang.org/t/pluto-undefvarerror/51480 "2020-12-08T21:29:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [December 8, 2020, 9:29pm UTC](https://discourse.julialang.org/t/pluto-undefvarerror/51480/1 "2020-12-08T21:29:56Z")

</div>

Hi,

I am using `Pluto` and got an `UndefVarError ` I cannot reproduce in VSCode.

If I run the following code in Pluto I get an error that `foo` is not defined:

```julia
begin
	foo = [1,2,3]
	all_foos = []
	for i in 1:5
		new_foo = @pipe sum(foo) |> repeat([_], 3)
		push!(all_foos, new_foo)
		foo = new_foo
	end
end

```

I found two solutions to the problem:

- put everything into a function
- use `global foo` in the for loop

Also, the code runs in VS Code.

Can someone explain to me if this is intended behavior in Pluto and if so why?

Thanks!

---

<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:** [December 9, 2020, 4:21am UTC](https://discourse.julialang.org/t/pluto-undefvarerror/51480/2 "2020-12-09T04:21:42Z")

</div>

[The for loop introduces a new local scope](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Local-Scope). Then what happens is that you are definining a variable `all_foos` in an outer scope, and then trying to modify a variable `all_foos` that is local to the for loop. Because you had not defined it, it throws an error (outer `all_foos` is different than inner `all_foos`).

For interactive usage, however, julia lets you use the same variable. They call this `soft scope` and you can read about it [here](https://julialang.org/blog/2020/08/julia-1.5-highlights/#the_return_of_soft_scope_in_the_repl). For some reason, [Pluto is not considered “interactive” for these purposes](https://github.com/fonsp/Pluto.jl/issues/379#issuecomment-689130899), so this fails as it would if you wrote this in a script and tried running it.

It is nice to be aware of this different behavior between interactive and non-interactive work, as it may bite at some point.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [December 9, 2020, 6:55am UTC](https://discourse.julialang.org/t/pluto-undefvarerror/51480/3 "2020-12-09T06:55:23Z")

</div>

It is even more complex:  
Since one of the latest releases, Pluto automatically wraps cells into functions (see [https://github.com/fonsp/Pluto.jl/pull/720](https://github.com/fonsp/Pluto.jl/pull/720)) for performance reasons. Thus, the scope should in principle behave as you have expected it.  
However, Pluto cannot wrap all cells into functions, excluded are e.g. cells with macros (your case - Pluto does not know which variables are modified inside macros) or inputs.  
Thus, in your concrete case all\_foos is not defined in the for-loop.  
You can try to replace the pipe-macro, then this code should work in the latest Pluto version.

---

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [December 9, 2020, 9:58am UTC](https://discourse.julialang.org/t/pluto-undefvarerror/51480/4 "2020-12-09T09:58:39Z")

</div>

Great, thanks for the helpful answers!
