# How to resize a 2D or higher dimensional array?

**URL:** <https://discourse.julialang.org/t/how-to-resize-a-2d-or-higher-dimensional-array/66055>\
**Category:** General Usage\
**Created:** [August 9, 2021, 3:56am UTC](https://discourse.julialang.org/t/how-to-resize-a-2d-or-higher-dimensional-array/66055 "2021-08-09T03:56:17Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [August 9, 2021, 4:53am UTC](https://discourse.julialang.org/t/how-to-resize-a-2d-or-higher-dimensional-array/66055/2 "2021-08-09T04:53:40Z")

</div>

First. The `global` in `global const` is redundant. Just `const` is sufficient.

Second. Instead of defining a global array variable for the workspace at the beginning of the module, it would be better to define a constructor function for the workspace, like the following simplified style.

```julia
module Foo

workspace(m, n) = Matrix{Float64}(undef, m, n)

function f(ws, x, y)
    ...calculation with workspace ws...
end

end

ws = Foo.workspace(100, 100)
z = Foo.f(ws, 1.2, 3.4)

```

Always use global variables by passing them as arguments to the function. It is better not to use a global variable in a function without passing it as an argument.

If the size of the workspace is determined by the problem and the algorithm, you can create an object `alg` describing the algorithm from the object `prob` describing the problem with `alg = SomeAlg(prob)`, and use the constructor `SomeAlg` to create a workspace of the required size in `alg`.

cf. [Problem-Algorithm-Solver style coding](https://discourse.julialang.org/t/function-depending-on-the-global-variable-inside-module/64322/10) and [Game of Life example](https://discourse.julialang.org/t/how-to-prevent-using-global-arrays-in-iteration-in-the-module/65857/12)

---

_[View the full topic](https://discourse.julialang.org/t/how-to-resize-a-2d-or-higher-dimensional-array/66055)._
