Scope in reduce

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

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:

UndefVarError: c not defined

use global:

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

1 Like