# Unable to use add\_agent!() function with keyword argument

**URL:** https://discourse.julialang.org/t/unable-to-use-add-agent-function-with-keyword-argument/65321
**Category:** General Usage
**Tags:** agents
**Created:** [July 26, 2021, 5:26pm UTC](https://discourse.julialang.org/t/unable-to-use-add-agent-function-with-keyword-argument/65321 "2021-07-26T17:26:50Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![imantha](https://avatars.discourse-cdn.com/v4/letter/i/6a8cbe/32.png) [@imantha](https://discourse.julialang.org/u/imantha)
#### Post date: [July 26, 2021, 5:26pm UTC](https://discourse.julialang.org/t/unable-to-use-add-agent-function-with-keyword-argument/65321/1 "2021-07-26T17:26:50Z")

</div>

Hi,

Just curious to find out if it is possible to use keyword argument with add\_agent!() function.  
When I tried adding a keyword argument, I got the following error : `LoadError: MethodError: No Method matching CasAgent(::Int64, ::Typle{Int64, Int64}; TS = 2)`

```julia
using Agents
mutable struct CasAgent <: AbstractAgent
    id::Int
    pos::Dims{2}
    TS::Int
end

space = GridSpace((10,10), periodic = false)
model = AgentBasedModel(
    CasAgent,
    space, 
    scheduler = random_activation
)

for pos in position(model)
    add_agent!(
        pos,
        model,
        TS = 2
    )
end

#Out > LoadError: MethodError: No Method matching CasAgent(::Int64, ::Typle{Int64, Int64}; TS = 2)

```

Of course removing the keyword would work fine. But I am wondering if there is a way to include a keyword.

```julia
for pos in position(model)
    add_agent!(
        pos,
        model,
        2
    )
end

#Out > AgentBasedModel with 100 agents of type CasAgent4

```

I also tried using the `@kwdef` from the base package but it didn’t work either

```julia
using Base::@kwdef
@kwdef mutable struct CasAgent <: AbstractAgent
    id::Int
    pos::Dims{2}
    TS::Int
end

for pos in position(model)
    add_agent!(
        pos,
        model,
        TS = 2
    )
end

#Out > LoadError: MethodError: No Method matching CasAgent(::Int64, ::Typle{Int64, Int64}; TS = 2)

```

---

<div class="post-metadata">

### Author: ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)
#### Post date: [July 26, 2021, 5:32pm UTC](https://discourse.julialang.org/t/unable-to-use-add-agent-function-with-keyword-argument/65321/2 "2021-07-26T17:32:13Z")

</div>

```julia
@kwdef mutable struct CasAgent <: AbstractAgent
    id::Int
    pos::Dims{2}
    TS::Int = 0
end

```

You need to provide the default value for `TS`. Otherwise, the compiler wouldn’t know how to process `CasAgent(1, (1, 1))`.

Alternatively, according to [Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/#Keyword-Arguments) you can define constructor with required keyword argument

```julia
mutable struct CasAgent <: AbstractAgent
    id::Int
    pos::Dims{2}
    TS::Int
end

CasAgent(id, pos; TS) = CasAgent(id, pos, TS)

```

---

<div class="post-metadata">

### Author: ![Libbum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/libbum/32/3935_2.png) [@Libbum](https://discourse.julialang.org/u/Libbum)
#### Post date: [July 26, 2021, 5:40pm UTC](https://discourse.julialang.org/t/unable-to-use-add-agent-function-with-keyword-argument/65321/3 "2021-07-26T17:40:05Z")

</div>

Perfect answer!

We use this exact method [in testing](https://github.com/JuliaDynamics/Agents.jl/blob/master/test/runtests.jl#L51).
