# How to pass struct fields as function arguments?

**URL:** <https://discourse.julialang.org/t/how-to-pass-struct-fields-as-function-arguments/24599>\
**Category:** New to Julia\
**Created:** [May 25, 2019, 2:36pm UTC](https://discourse.julialang.org/t/how-to-pass-struct-fields-as-function-arguments/24599 "2019-05-25T14:36:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elmer\_Cusipuma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmer_cusipuma/32/11958_2.png) [@Elmer\_Cusipuma](https://discourse.julialang.org/u/Elmer_Cusipuma)\
**Post date:** [May 25, 2019, 2:36pm UTC](https://discourse.julialang.org/t/how-to-pass-struct-fields-as-function-arguments/24599/1 "2019-05-25T14:36:00Z")

</div>

Example

```julia
struct variables
   a :: Integer
   b :: Float64
   c :: Integer
end

function f(a::Integer,b::Float64,c::Integer)
   d = a + b + c
   println(d)
end

var = variables(1,2.5,3)

f(var) # ?

```

How can I pass a structure as an input argument?

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [May 25, 2019, 2:56pm UTC](https://discourse.julialang.org/t/how-to-pass-struct-fields-as-function-arguments/24599/2 "2019-05-25T14:56:58Z")

</div>

EDIT 2: Here is how you _actually_ should do this:

```julia
function f(var::variables)
  d = var.a + var.b + var.c
  println(d)
end

f(var)

```

You could _also_ do this, although you _shouldn’t_, and it can’t be properly inferred by the compiler so it will involve dynamic dispatch:

`f(getfield.(Ref(var), fieldnames(variables))...)`

EDIT: Brief description:

Gets a `Tuple` of `Symbol`s which refer to the field names of the `variables` struct type via `fieldnames(variables)`, then uses a broadcasted `getfield` to get the values of those fields out of the `var` struct, and then splats them into the call to `f`.

You could instead do this as an `@generated` function if you want it to be inferrable.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 25, 2019, 3:21pm UTC](https://discourse.julialang.org/t/how-to-pass-struct-fields-as-function-arguments/24599/3 "2019-05-25T15:21:35Z")

</div>

FWIW, this seems like an unusual thing to do. For example, it changes behaviour when reordering the fields in the struct which isn’t very nice.

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [May 25, 2019, 3:23pm UTC](https://discourse.julialang.org/t/how-to-pass-struct-fields-as-function-arguments/24599/4 "2019-05-25T15:23:58Z")

</div>

Agreed, the best thing to do is to just dispatch on the input as `f(var::variables) = ...`. I think I misread the intent of the OP 😆

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [May 25, 2019, 6:35pm UTC](https://discourse.julialang.org/t/how-to-pass-struct-fields-as-function-arguments/24599/5 "2019-05-25T18:35:54Z")

</div>

This is a very good case for [Parameters.jl](https://github.com/mauro3/Parameters.jl) if you have a lot of variables:

```julia
using Parameters

@with_kw struct variables
   a :: Integer
   b :: Float64
   c :: Integer
end

function f(s::variables)
    @unpack_variables s
    d = a + b + c
    println(d)
end

julia> f(variables(1,2.5,3))
6.5

```

---

<div class="post-metadata">

**Author:** ![jman87](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jman87/32/1423_2.png) [@jman87](https://discourse.julialang.org/u/jman87)\
**Post date:** [September 14, 2022, 1:52pm UTC](https://discourse.julialang.org/t/how-to-pass-struct-fields-as-function-arguments/24599/6 "2022-09-14T13:52:13Z")

</div>

Hi @Ronis_BR,

This package looks very useful as I’m just really starting to use Structs more. Unfortunately, the @unpack\_variables macro does not appear to be working. I’d like to know if I’m doing something wrong or if something has changed since your post here.

After installing the Parameters package, I created a script called parameters\_test.jl containing the following:

```julia
using Parameters

@with_kw struct v_type
    a::Int
    b::Float64
    c::Int
end

v = v_type(11, 2.2, 33)

function vtest(a::Int, b::Float64, c::Int)
    println("a = $a")
    println("b = $b")
    println("c = $c")
    return (a+b+c)
end

function vtest(abc::v_type)
    @unpack_variables abc
    println("a = $a")
    println("b = $b")
    println("c = $c")
    return (a+b+c)
end

```

When I execute this, however, I get the following:

```julia
julia> include("parameters_test.jl")
ERROR: LoadError: UndefVarError: @unpack_variables not defined
Stacktrace:
 [1] top-level scope
   @ :0
 [2] include(fname::String)
   @ Base.MainInclude ./client.jl:476
 [3] top-level scope
   @ REPL[1]:1
in expression starting at /Users/home/parameters_test.jl:19
in expression starting at /Users/home/parameters_test.jl:18

```

FYI, I’m running Julia 1.8.0 (2022-08-17). Appreciate any feedback.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [September 14, 2022, 1:58pm UTC](https://discourse.julialang.org/t/how-to-pass-struct-fields-as-function-arguments/24599/7 "2022-09-14T13:58:50Z")

</div>

Hi @jman87 !

I think the name of the macro will be `@unpack_v_type` in your case. It is always `@unpack_<name of the structure>`.
