# Using the same parameter n times in a function call

**URL:** https://discourse.julialang.org/t/using-the-same-parameter-n-times-in-a-function-call/89833
**Category:** General Usage
**Tags:** question
**Created:** [November 6, 2022, 1:44am UTC](https://discourse.julialang.org/t/using-the-same-parameter-n-times-in-a-function-call/89833 "2022-11-06T01:44:26Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Zackyzz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zackyzz/32/44173_2.png) [@Zackyzz](https://discourse.julialang.org/u/Zackyzz)
#### Post date: [November 6, 2022, 1:44am UTC](https://discourse.julialang.org/t/using-the-same-parameter-n-times-in-a-function-call/89833/1 "2022-11-06T01:44:26Z")

</div>

Hello!  
I am struggling to find a way to “automate” this type of function call:

```julia
using LinearAlgebra
x = [rand() rand(); rand() rand()]
kron(x, x) #n=2
kron(x, x, x) #n=3
kron(x, x, x, x) #n=4

```

Basically, having a 2x2 matrix “x” I would like to use the kronecker product of x with itself n times.  
Is there a way to achieve this, perhaps in a manner like:

`kron(repeat(x, n))`

Of course not with the repeat function, but perhaps with something else.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [November 6, 2022, 1:53am UTC](https://discourse.julialang.org/t/using-the-same-parameter-n-times-in-a-function-call/89833/2 "2022-11-06T01:53:57Z")

</div>

```julia
kron(fill(x, 3)...)

```

---

<div class="post-metadata">

### Author: ![Zackyzz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zackyzz/32/44173_2.png) [@Zackyzz](https://discourse.julialang.org/u/Zackyzz)
#### Post date: [November 6, 2022, 2:04am UTC](https://discourse.julialang.org/t/using-the-same-parameter-n-times-in-a-function-call/89833/3 "2022-11-06T02:04:48Z")

</div>

Thank you! “…” looks like an interesting and useful operator… haven’t encountered (or used) in other programming languages.

---

<div class="post-metadata">

### Author: ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)
#### Post date: [November 6, 2022, 2:17am UTC](https://discourse.julialang.org/t/using-the-same-parameter-n-times-in-a-function-call/89833/4 "2022-11-06T02:17:36Z")

</div>

Hey @Zackyzz , glad you got your question answered! Also, welcome to the Julia Community – saw it was your first post here on Discourse! 👋 Hope you enjoy your time here!

Yea, this syntax is interesting – it is called splatting and you can learn more about it here: [Essentials · The Julia Language](https://docs.julialang.org/en/v1/base/base/#)… (I am a fan of it but sometimes, it is not as efficient as maps or otherwise).

---

<div class="post-metadata">

### Author: ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)
#### Post date: [November 6, 2022, 2:25am UTC](https://discourse.julialang.org/t/using-the-same-parameter-n-times-in-a-function-call/89833/5 "2022-11-06T02:25:58Z")

</div>

I am a big fan of “splat” and “slurp” as Julia’s most comical terminology.

---

<div class="post-metadata">

### Author: ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)
#### Post date: [November 6, 2022, 11:00am UTC](https://discourse.julialang.org/t/using-the-same-parameter-n-times-in-a-function-call/89833/6 "2022-11-06T11:00:55Z")

</div>

In the case of `kron` and other associative operations, you can also do `reduce(kron, fill(x, n))`. That’s useful to know for many other associative functions like `hcat`, `union` etc.

---

<div class="post-metadata">

### Author: ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)
#### Post date: [November 6, 2022, 11:03am UTC](https://discourse.julialang.org/t/using-the-same-parameter-n-times-in-a-function-call/89833/7 "2022-11-06T11:03:15Z")

</div>

The reduce trick is useful when `n` is large or not known at compile time to avoid the performance issues of splatting and slurping (`...`).

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [November 6, 2022, 7:53pm UTC](https://discourse.julialang.org/t/using-the-same-parameter-n-times-in-a-function-call/89833/8 "2022-11-06T19:53:55Z")

</div>

```julia
kronn(x,n)=n==2 ? kron(x,x) : kron(x, kronn(x,n-1))

```

```julia
kronn(x,n)= n==1 ? x : kron(x, kronn(x,n-1))

```

this is correct the above are incorrect (kron is not commutative)

```julia
kronn(x,n)= n==1 ? x : kron(kronn(x,n-1),x)

```
