Error: "Expected non-array type after array in return type"

I am receiving the following error when running the Stan program at the bottom of this post in RStan:

Syntax error in ‘string’, line 5, column 22 to column 23, parsing error:
Expected non-array type after array in return type

The error is due to the object returned from my call to the fill_0s() function defined in the transformed parameters block. However, I don’t know what is causing the error, since my function is supposed to take an array of integers as an input and output an array of integers…but the error tells me it’s expecting NOT to receive this. Any ideas on what I’m doing wrong?

  int x; 

transformed data{

// defines: `fill_0s` function
// loops through a size x array of integers and replaces values with 0, returning
// an array of integers
  array[x] int fill_0s(array[x] int in_array){
    int z = size(in_array);
    for (i in 1:z){
      in_array[i] = 0;

  array[x] int new_array;
  new_array = fill_0s(new_array);

Someone with better understanding of Stan errors should respond as well, but this should fix itself if you change your function’s argument and return types. You should swap out array[x] int with array[] int at both spots in the function definition.

The reason this works is because you do not need to declare the size of vectors and matrices in argument and return types in functions. Strangely, from the User’s Guide section on basic functions (20.1 Basic functions | Stan User’s Guide):

> Function argument and return types for vector and matrix types are not declared with their sizes, unlike type declarations for variables. 

This is echoed in the reference manual section on Argument Types (9.4 Argument types and qualifiers | Stan Reference Manual):

The dimensions for vectors and matrices are not included

Similarly, you can scroll down and see the following plus further detail:

Array arguments have their own syntax, which follows that used in this manual for function signatures.

1 Like