# Scope in reduce

**URL:** <https://discourse.julialang.org/t/scope-in-reduce/48482>\
**Category:** General Usage\
**Created:** [October 16, 2020, 9:19am UTC](https://discourse.julialang.org/t/scope-in-reduce/48482 "2020-10-16T09:19:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alligatorme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alligatorme/32/6962_2.png) [@alligatorme](https://discourse.julialang.org/u/alligatorme)\
**Post date:** [October 16, 2020, 9:19am UTC](https://discourse.julialang.org/t/scope-in-reduce/48482/1 "2020-10-16T09:19:57Z")

</div>

It sesms that the global variable is not avaliable in function of reduce’s first augument.

```julia
a=range(1,10,step=1)
c=2
function ad(a,b)
    c=c*c
    return a+b+c
end
reduce(ad,a)

```

the result shows:

```julia
UndefVarError: c not defined

```

---

<div class="post-metadata">

**Author:** ![MarcMush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcmush/32/18006_2.png) [@MarcMush](https://discourse.julialang.org/u/MarcMush)\
**Post date:** [October 16, 2020, 9:24am UTC](https://discourse.julialang.org/t/scope-in-reduce/48482/2 "2020-10-16T09:24:25Z")

</div>

use `global`:

```julia
c=2
function ad(a,b)
    global c=c*c
    return a+b+c
end

```

this will change the value of c, if you don’t want to change it, use another variable name  
also use ``` to display nice code here
