# Result of a function is a 3x6 matrix

**URL:** <https://discourse.julialang.org/t/result-of-a-function-is-a-3x6-matrix/69102>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 2, 2021, 8:56am UTC](https://discourse.julialang.org/t/result-of-a-function-is-a-3x6-matrix/69102 "2021-10-02T08:56:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Niceno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niceno/32/29648_2.png) [@Niceno](https://discourse.julialang.org/u/Niceno)\
**Post date:** [October 2, 2021, 8:56am UTC](https://discourse.julialang.org/t/result-of-a-function-is-a-3x6-matrix/69102/1 "2021-10-02T08:56:10Z")

</div>

Dear all,

I started to use Julia a week ago, by porting one of my existing computational fluid dynamics solvers to Julia. A subroutine I am currently working on is creating a 3x6 matrix of Float64s. In Fortran or C++ I would send a reference to the matrix to a subroutine or function, it would work on its contents and results would be available in the body of the code which called it.

I am not quite sure how to approach it in Julia. I understand that Julia is all about returning values from function calls, but I somehow feel that forming a 3x6 matrix inside a function and returning it as a result would create an overhead in terms of memory allocation/deallocation and copying. Shall I put it inside a container (struct) and send that to the function instead? Would that be in line with Julia’s best practices? I don’t know.

I would appreciate any hint.

```
Cheers

```

---

<div class="post-metadata">

**Author:** ![mike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike/32/39_2.png) [@mike](https://discourse.julialang.org/u/mike)\
**Post date:** [October 2, 2021, 9:02am UTC](https://discourse.julialang.org/t/result-of-a-function-is-a-3x6-matrix/69102/2 "2021-10-02T09:02:05Z")

</div>

> [@Niceno](#):
>
> but I somehow feel that forming a 3x6 matrix inside a function and returning it as a result would create an overhead in terms of memory allocation/deallocation and copying

[GitHub - JuliaArrays/StaticArrays.jl: Statically sized arrays for Julia](https://github.com/JuliaArrays/StaticArrays.jl) may help here rather than using standard `Array`s. You could also just pass it to your function and mutate it like you would in Fortran or C++, there’s nothing particularly wrong with doing that.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 2, 2021, 9:58am UTC](https://discourse.julialang.org/t/result-of-a-function-is-a-3x6-matrix/69102/3 "2021-10-02T09:58:18Z")

</div>

> [@Niceno](#):
>
> I am not quite sure how to approach it in Julia. I understand that Julia is all about returning values from function calls, but I somehow feel that forming a 3x6 matrix inside a function and returning it as a result would create an overhead in terms of memory allocation/deallocation and copying. Shall I put it inside a container (struct) and send that to the function instead? Would that be in line with Julia’s best practices? I don’t know.

A regular array is already a mutable container - no need for just wrapping in an additional struct (though using the struct to bundle multiple arrays into one is good).

You can just assign to the indices of a passed array and the values will be visible outside:

```julia
julia> a = zeros(3,6)
3×6 Matrix{Float64}:
 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0

julia> function myFunc!(arr)
         arr[1,2] = 5.0
       end
myFunc! (generic function with 1 method)

julia> myFunc!(a)
5.0

julia> a
3×6 Matrix{Float64}:
 0.0 5.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0 0.0

```

Note that `5.0` in the last display.

The exclamation mark is just a notational convention, to signal to users that the function is modifying one or more of its arguments. This behavior works for any mutable struct/container.

---

<div class="post-metadata">

**Author:** ![Hand548](https://avatars.discourse-cdn.com/v4/letter/h/6f9a4e/32.png) [@Hand548](https://discourse.julialang.org/u/Hand548)\
**Post date:** [October 8, 2021, 8:40am UTC](https://discourse.julialang.org/t/result-of-a-function-is-a-3x6-matrix/69102/4 "2021-10-08T08:40:36Z")

</div>

inbuilt functions in general are fine. But cell is a much more versatile datatype than matrix. As such it should not be a surprise that usually it is more efficient to operate on the matrix, rather than converting it to a cell, operating on the cell, and converting it to a matrix. – For small problems efficiency may not be a concern, but in that case I would still argue that it is best practice for code clarity, to stick to matrices unless there is a compelling reason not to[.](https://www.upsers.page/)
