# Tensor product inside function

**URL:** <https://discourse.julialang.org/t/tensor-product-inside-function/30197>\
**Category:** New to Julia\
**Created:** [October 22, 2019, 10:38pm UTC](https://discourse.julialang.org/t/tensor-product-inside-function/30197 "2019-10-22T22:38:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Veenty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/veenty/32/50940_2.png) [@Veenty](https://discourse.julialang.org/u/Veenty)\
**Post date:** [October 22, 2019, 10:38pm UTC](https://discourse.julialang.org/t/tensor-product-inside-function/30197/1 "2019-10-22T22:38:52Z")

</div>

So I have the following code

```
function CC_trap(X::Array{Float64,2}, tag::Int64)
    a = 0.0

    for i=1:size(X)[1]
      	    for j=1:size(X)[2]

		    global a += weights_CC[i]*X[i,j]
	    end
    end

    a = result_integral*Parameters.dv[tag]

end

```

But I’m getting the error  
syntax: `global a`: a is a local variable in its enclosing scope

What would be the proper Julia way of doing this?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [October 23, 2019, 5:05am UTC](https://discourse.julialang.org/t/tensor-product-inside-function/30197/3 "2019-10-23T05:05:59Z")

</div>

There are global variables in your code. This is the way to make the function compile.

```julia
function CC_trap(X::Array{Float64,2}, tag::Int64)
    a = 0.0
    global weights_CC
    global Parameters
    for i=1:size(X)[1]
        for j=1:size(X)[2]
            a += weights_CC[i]*X[i,j]
        end
    end
    a = result_integral*Parameters.dv[tag]
end

```

However, I have some doubts about using global variables in the first place. They rarely lead to efficient (type-stable) code. Usually they can be avoided, and it is worthwhile to think about ways in which that could be done. It usually leads to a better design.
