# Returning Arrays from Functions - Recommended Approach?

**URL:** <https://discourse.julialang.org/t/returning-arrays-from-functions-recommended-approach/75448>\
**Category:** General Usage\
**Tags:** argument, arrays, functions\
**Created:** [January 30, 2022, 4:37am UTC](https://discourse.julialang.org/t/returning-arrays-from-functions-recommended-approach/75448 "2022-01-30T04:37:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)\
**Post date:** [January 30, 2022, 4:37am UTC](https://discourse.julialang.org/t/returning-arrays-from-functions-recommended-approach/75448/1 "2022-01-30T04:37:22Z")

</div>

I have been working through an exercise of reviewing and re-reviewing some code I have that solves a one-dimensional system of coupled ODEs using finite differences (just as a toy problem to get better with Julia). I have managed to squeeze every single allocation out of the loops of my calculations by using mutating functions, generators, and pre-allocated arrays.

Presently, all the code is in a single function (called `solve`). I think it would be better to re-organize that code into functions within `solve()` instead of being one monolithic chunk of code.

The problem I have is that I need to pre-allocate 13 arrays of various size. If I were to move all the pre-allocation code into a function (e.g. `setup()`), something like the pseudo code below, I don’t know what the best way to return a bunch of variably sized arrays from a function is. As a tuple of functions? Create a struct where each field is one of the arrays and return that?

Hopefully my question makes sense. Perhaps I am over complicating it but I would appreciate any advice or thoughts on how to accomplish this.

```julia
function solve()
pre_allocated_arrays = setup() # <- calling the setup function to generate pre-allocated arrays
# Hot loop
end
```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [January 30, 2022, 2:08pm UTC](https://discourse.julialang.org/t/returning-arrays-from-functions-recommended-approach/75448/2 "2022-01-30T14:08:53Z")

</div>

I usually use a struct of arrays that I pass to every function as first parameter, because in this way it can be re-used for multiple calls to solve() and is allocated only once. You can see a complex example here: [KiteModels.jl/KiteModels.jl at main · ufechner7/KiteModels.jl · GitHub](https://github.com/ufechner7/KiteModels.jl/blob/main/src/KiteModels.jl) line 107ff

I usually have functions like:  
clear(state)  
init(state)  
solve(state)

As long as there are only arrays in the struct there is no need for a mutable struct, a simple (immutable) struct is good enough.

---

<div class="post-metadata">

**Author:** ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)\
**Post date:** [January 30, 2022, 7:05pm UTC](https://discourse.julialang.org/t/returning-arrays-from-functions-recommended-approach/75448/3 "2022-01-30T19:05:12Z")

</div>

Thanks for the example. I implemented your approach - I have an `init` function that generates the struct containing all the arrays then I pass it to my `solve` function which has the hot loop. Thank you!

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [January 30, 2022, 10:38pm UTC](https://discourse.julialang.org/t/returning-arrays-from-functions-recommended-approach/75448/4 "2022-01-30T22:38:03Z")

</div>

You are welcome!
