# Passing integer array into Stan function

**URL:** <https://discourse.mc-stan.org/t/passing-integer-array-into-stan-function/28985>\
**Category:** General\
**Tags:** rstan\
**Created:** [September 30, 2022, 7:22pm UTC](https://discourse.mc-stan.org/t/passing-integer-array-into-stan-function/28985 "2022-09-30T19:22:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JoLee](https://avatars.discourse-cdn.com/v4/letter/j/278dde/32.png) [@JoLee](https://discourse.mc-stan.org/u/JoLee)\
**Post date:** [September 30, 2022, 7:22pm UTC](https://discourse.mc-stan.org/t/passing-integer-array-into-stan-function/28985/1 "2022-09-30T19:22:50Z")

</div>

I have a function in Stan that takes as input an array of integers, Index. The function uses the indices to sum over a vector:

I.e.

```nohighlight
sum(II[Index[i]:])

```

I input the Index array in the data block

```nohighlight
int M3;
int Index[M3];

```

and input the array of integers into the function as

```nohighlight
(...,int Index)

```

I am receiving the following error

```nohighlight
Too many indexes, expression dimensions=0, indexes found=1

```

Is this because Stan only allows a single integer value to be passed to the function? I need the array to be integers because Index[i] must be an integer to perform the following.

```nohighlight
sum(II[Index[i]:])

```

So I cannot define Index to be of type “vector”.

Does anyone know how I can pass an array of integers into a function? Or if the error means something else?

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [September 30, 2022, 8:19pm UTC](https://discourse.mc-stan.org/t/passing-integer-array-into-stan-function/28985/2 "2022-09-30T20:19:26Z")

</div>

The variable named `Index` in your data block and the function parameter `Index` are independent of each other. As such, the function signature you wrote down specified the type as `int` for the function parameter. If you want this to be an int array, you should write `(..., array[] int Index)`

---

<div class="post-metadata">

**Author:** ![JoLee](https://avatars.discourse-cdn.com/v4/letter/j/278dde/32.png) [@JoLee](https://discourse.mc-stan.org/u/JoLee)\
**Post date:** [October 3, 2022, 5:51pm UTC](https://discourse.mc-stan.org/t/passing-integer-array-into-stan-function/28985/3 "2022-10-03T17:51:44Z")

</div>

I tried to input as

```nohighlight
array[] int Index

```

and received an error

```nohighlight
PARSER EXPECTED: <argument declaration or close paren ) to end argument declarations>

```

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [October 3, 2022, 5:57pm UTC](https://discourse.mc-stan.org/t/passing-integer-array-into-stan-function/28985/4 "2022-10-03T17:57:45Z")

</div>

If you’re using an older version (such as rstan from CRAN) you may need to make it `int[] index` instead.
