# Organizing many function returns

**URL:** <https://discourse.julialang.org/t/organizing-many-function-returns/66569>\
**Category:** New to Julia\
**Tags:** data\_structures, functions\
**Created:** [August 17, 2021, 8:44pm UTC](https://discourse.julialang.org/t/organizing-many-function-returns/66569 "2021-08-17T20:44:13Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![PatrickMcFarlane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patrickmcfarlane/32/50027_2.png) [@PatrickMcFarlane](https://discourse.julialang.org/u/PatrickMcFarlane)\
**Post date:** [August 17, 2021, 8:44pm UTC](https://discourse.julialang.org/t/organizing-many-function-returns/66569/1 "2021-08-17T20:44:14Z")

</div>

I’m writing a dynamic optimization algorithm for an economics application with search and matching and unemployment. The algorithm is structured as two functions. There’s an outer function that iterates until finding a fixed point in the optimized value functions. It feeds a current guess for the value functions to an inner function that computes optimal policies for control variables and updates the value functions.

The inner function will have several returns (all multidimensional arrays) corresponding to optimal policy functions, value functions, and derivatives of the value functions with respect to endogenous state variables.

Currently there are 14 separate returns.

Is there a way to organize these returns so that when calling the function, one doesn’t need to keep track of the ordering of the returns?

My first thought was to use a named tuple to contain all these returns, but then I thought the immutability of named tuples might cause problems because I need to call the inner function a large number of times and have its returns mutate a variable in the outer fixed point function.

Performance is a key concern because this algorithm is part of a very large and computationally intensive model.

Does anyone have suggestions?

---

<div class="post-metadata">

**Author:** ![jlbosse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlbosse/32/11274_2.png) [@jlbosse](https://discourse.julialang.org/u/jlbosse)\
**Post date:** [August 17, 2021, 9:33pm UTC](https://discourse.julialang.org/t/organizing-many-function-returns/66569/2 "2021-08-17T21:33:44Z")

</div>

Simply define a new type containing all of the return values? I.e.

```julia
mutable struct ResultType{T1,T2,...}
    rval1::T1
    rval::T2
    ...
end

function innerfunc(args)
    rval1 = dostuff(args)
    rval2 = domorestuff(args)
    ...
    return ReturnType(rval1, rval2, ...)
end

```

Note that you will have to be careful about the field types or `ReturnType` and make sure that they all resolve to concrete types.

---

<div class="post-metadata">

**Author:** ![jlbosse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlbosse/32/11274_2.png) [@jlbosse](https://discourse.julialang.org/u/jlbosse)\
**Post date:** [August 17, 2021, 9:40pm UTC](https://discourse.julialang.org/t/organizing-many-function-returns/66569/3 "2021-08-17T21:40:13Z")

</div>

About the named tuples: Do you need to mutate the contents of the arrays, or will you need entirely new arrays? Because the former is still possible, even if the array is in a named tuple. See e.g.

```julia
mytuple = (arr=[1,2,3], scalar=1.0)
mytuple.arr[1] = 2 # works
mytuple.arr = [3,4,5] # does not work
mytuple.arr .= [3,4,5] # also works
mytuple.scalar = 2. # does not work
resize!(mytuple.arr, 2) # even this works!

```

---

<div class="post-metadata">

**Author:** ![PatrickMcFarlane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patrickmcfarlane/32/50027_2.png) [@PatrickMcFarlane](https://discourse.julialang.org/u/PatrickMcFarlane)\
**Post date:** [August 18, 2021, 12:00am UTC](https://discourse.julialang.org/t/organizing-many-function-returns/66569/4 "2021-08-18T00:00:54Z")

</div>

Thanks for the speedy reply! I would need to mutate all of the elements in the arrays. Your code snippet suggests this is possible by using something like `mytuple.arr .= newarr`. How would I work this into a function call though? Would I pass in the tuple with arrays to be mutated. Something like:

```julia
function !innerfunc(args, mytuple)

    newarr = dostuff(args)

    mytuple.arr .= newarr

end

```

Is there a way to do it without requiring the tuple to be one of the function arguments?

Also, any thoughts on pros/cons of this approach compared to the mutable struct idea in your previous post?

One challenge I think would be that I am using a trick to allow me to change the precision of all floats in the model. I declare a constant `prec` with the type of precision I want (e.g., `const prec = 1f0`) and then use `fill`, `oftype()`, `typeof()`, etc., to ensure that all other floats in the model have that same precision as `prec`. I imagine it would be tricky to implement that trick for the mutable struct.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 18, 2021, 12:52am UTC](https://discourse.julialang.org/t/organizing-many-function-returns/66569/5 "2021-08-18T00:52:19Z")

</div>

> [@PatrickMcFarlane](#):
>
> Is there a way to do it without requiring the tuple to be one of the function arguments?

Pass `mytuple.arr` as the argument. Arrays are always passed by reference.

---

<div class="post-metadata">

**Author:** ![viraltux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viraltux/32/15236_2.png) [@viraltux](https://discourse.julialang.org/u/viraltux)\
**Post date:** [August 18, 2021, 3:15am UTC](https://discourse.julialang.org/t/organizing-many-function-returns/66569/6 "2021-08-18T03:15:32Z")

</div>

> [@PatrickMcFarlane](#):
>
> My first thought was to use a named tuple to contain all these returns, but then I thought the immutability of named tuples

A Dict would act like a named tuple

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [August 18, 2021, 4:25am UTC](https://discourse.julialang.org/t/organizing-many-function-returns/66569/7 "2021-08-18T04:25:02Z")

</div>

> [@PatrickMcFarlane](#):
>
> Also, any thoughts on pros/cons of this approach compared to the mutable struct idea in your previous post?

I think you should use an **immutable** struct for your case. You have enough fields that it would be nice to have it an actual type. Named tuples are better for simple code. These can be immutable for the same reason the named tuple fields could be. You modify the existing arrays, rather than assigning a new one. Make a constructor that creates empty values of the right sizes.

A few idiomatic points.

- What you are talking about here is some sort of inplace solutions on a preallocated structure. So maybe name your type accordingly.
- by convention, you add the `!` to the inplace solution function at the end, not the beginning.
- by convention you make the mutated argument the first one, so I would call it

```julia
results = ResultTyoe(???) # do you need sizes?

    # then in loop, etc....
    innerfunc!(results, args)

```

But… Make 100% sure this is all worth it. Preallocating and doing things inplace often doesn’t help until things get large, and can hurt performance otherwise.

---

<div class="post-metadata">

**Author:** ![jlbosse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlbosse/32/11274_2.png) [@jlbosse](https://discourse.julialang.org/u/jlbosse)\
**Post date:** [August 18, 2021, 8:55am UTC](https://discourse.julialang.org/t/organizing-many-function-returns/66569/8 "2021-08-18T08:55:20Z")

</div>

Also note that you can directly do

```julia
function innerfunc!(args, mytuple)

    mytuple.arr .= dostuff(args)

end

```

which might save you some allocations (not sure if the compiler would just turn both versions into the same thing anyway).

Re pros / cons: I concur with @jlperla that in this case you have enough fields to make it an actual type. Also, if you don’t need to change the fields of your struct, but only the contents of arrays an immutable struct is probably better.

Re passing in the tuple: If you want to preallocate your arrays, there is no way around passing them into your function.

---

<div class="post-metadata">

**Author:** ![PatrickMcFarlane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patrickmcfarlane/32/50027_2.png) [@PatrickMcFarlane](https://discourse.julialang.org/u/PatrickMcFarlane)\
**Post date:** [August 18, 2021, 12:10pm UTC](https://discourse.julialang.org/t/organizing-many-function-returns/66569/9 "2021-08-18T12:10:22Z")

</div>

Okay thank for the advice everyone. I’ll go with an immutable struct.

For one of the steps in the algorithm, I’ll need to compare old and new values of some of the arrays to check convergence. Initially, I was thinking this would be done in the outer function, but it seems like to implement that, I would need to add additional fields to the struct (e.g., `arraynew` and `arrayold`) which would seem to increase memory allocations.

Instead I could check convergence in the inner function before mutating the arrays and have the inner function return a boolean indicating convergence.

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [August 18, 2021, 1:03pm UTC](https://discourse.julialang.org/t/organizing-many-function-returns/66569/10 "2021-08-18T13:03:52Z")

</div>

I think that design starts coupling the inner and outer parts of the loop a little too much.

You could just allocate two of these at the beginning, then have a `prev_results` in the outer loop of the same type, and just assign into it before calling the inner loop, etc

---

<div class="post-metadata">

**Author:** ![PatrickMcFarlane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patrickmcfarlane/32/50027_2.png) [@PatrickMcFarlane](https://discourse.julialang.org/u/PatrickMcFarlane)\
**Post date:** [August 18, 2021, 1:45pm UTC](https://discourse.julialang.org/t/organizing-many-function-returns/66569/11 "2021-08-18T13:45:03Z")

</div>

Makes sense.

My initial concern was with economizing on memory allocations and storage space more generally.  
When I run the model at full scale and with 64 bit floats, the arrays will be on the order of 650mb each.

But I realized when writing code this morning that even doing the comparison within the inner function requires holding “old” results in a temporary array so there’s really no way to avoid those allocations and, as you suggest, it’s a cleaner design to have the comparison in the outer function.

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [August 18, 2021, 8:44pm UTC](https://discourse.julialang.org/t/organizing-many-function-returns/66569/12 "2021-08-18T20:44:58Z")

</div>

Those sound large enough for it to matter. Here is a contrived example for some of the inplace tricks:

```julia

using Parameters, LinearAlgebra
struct MyResults{T}
    x::Array{T,1}
    A::Array{T,2}
end
MyResults(N) = MyResults(Array{Float64,1}(undef, N), Array{Float64,2}(undef, N, N))    

#Support for inplace copying.
import Base.copy!
function copy!(dst::MyResults, src::MyResults)
    dst.x .= src.x
    dst.A .= src.A
end

# By convention, name has ! to denote mutating, and mutate first argument
function calculate_results!(results, val, params)
    @unpack N, b, C = params
    B = rand(N,N) # Contrived. Assume complicated
    lmul!(val, B) # val * B -> B inplace, no allocation
    mul!(results.A, B, C) # B * C -> results.A
    ldiv!(results.x, factorize(results.A), b) # x = A \ b inplace
end

# Some iterative algorithm
function iterate_values(vals, params)    
    @unpack N = params
    
    # preallocate
    results = MyResults(N)
    prev_results = MyResults(N)
    norms = similar(vals, length(vals) - 1)
        
    for (i, val) in enumerate(vals)
        calculate_results!(results, val, params)
        if i > 1
            norms[i-1] = norm(results.x- prev_results.x)            
            println("|x_new - x_old| = ", norms[i-1])
        end
        copy!(prev_results, results)
    end
end

```

Hopefully that is helpful. Note that I also tried to point out a few of the other things such as using inplace versions of the linsolve, multiplication, etc. Those may or may not be useful in your specific case, but when things get large you should always think about those sorts of things.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 18, 2021, 9:30pm UTC](https://discourse.julialang.org/t/organizing-many-function-returns/66569/13 "2021-08-18T21:30:10Z")

</div>

@jlperla, could you please complete the nice example so that it is self-contained / executable?

For instance, it runs if one adds the following lines but not sure if this is the correct way:

```julia
@with_kw struct parameters
    N::Int64 = 1000
    b::Array{Float64,1} = Array{Float64}(undef, N)
    C::Array{Float64,2} = Array{Float64,2}(undef, N, N)
end

N = 500
params = parameters(N = N, b = rand(N), C = rand(N,N))
vals = LinRange(1, 10, 15)
iterate_values(vals, params)

```

Thank you!

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [August 18, 2021, 9:45pm UTC](https://discourse.julialang.org/t/organizing-many-function-returns/66569/14 "2021-08-18T21:45:16Z")

</div>

@rafael.guerra I just added a variation of it into some lecture notes: [https://github.com/QuantEcon/lecture-julia.myst/blob/main/lectures/tools\_and\_techniques/numerical\_linear\_algebra.md#patterns-for-preallocated-caches-of-results](https://github.com/QuantEcon/lecture-julia.myst/blob/main/lectures/tools_and_techniques/numerical_linear_algebra.md#patterns-for-preallocated-caches-of-results)

This won’t be live and compiled to HTML/jupyter for a few weeks, but you can see the code there

---

<div class="post-metadata">

**Author:** ![PatrickMcFarlane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patrickmcfarlane/32/50027_2.png) [@PatrickMcFarlane](https://discourse.julialang.org/u/PatrickMcFarlane)\
**Post date:** [August 19, 2021, 2:27pm UTC](https://discourse.julialang.org/t/organizing-many-function-returns/66569/15 "2021-08-19T14:27:51Z")

</div>

Thanks so much for this. Super helpful!
