I am writing a user-defined function to take a vector of unknown length and to return a vector of the same length the elements of which are the result of applying a simple function to each element of the input.

My problem is that the parser reports type mismatch.

```
functions{
real U(vector[] x, real lo){
int N = size(x);
vector[N] result;
for(i in 1:N){
result[i] = step( x[i] - lo );
}
return result;
}
```

The parser reports

SYNTAX ERROR, MESSAGE(S) FROM PARSER:

No matches for:

step(vector)

Available argument signatures for step:

step(real)

I realise that `step`

requires a real argument, but I don’t see how to extract each element of `x`

in turn as a real.

Oh, that’s a bit confusing. Without a size `vector[]`

is interpreted as an *array* of vectors. The function signature you need is

```
vector U(vector x, real lo) {
```

No `[]`

anywhere.

1 Like

Thanks. I spend 99% of my time in R and 1% only in Stan and find it hard to recall all the differences.

But I still have a problem:

```
functions{
real U(vector x, real lo){
int N = num_elements(x);
vector[N] result;
for(i in 1:N){
result[i] = step( x[i] - lo );
}
return result;
}
```

is rejected also:

```
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Mismatched array dimensions.Base type mismatch. Returned expression does not match return type
LHS type = real; RHS type = vector
Improper return in body of function.
error in 'model6d9e7451ed81_mre' at line 10, column 0
-------------------------------------------------
8: return result;
9: }
^
-------------------------------------------------
```

`result`

is a vector but you declared the function returns a `real`

. The function signature you need is

```
vector U(vector x, real lo) {
```

Note the `vector`

before `U`

.

1 Like

Very many thanks. Works perfectly now.