# User-defined recursive function error

I am trying to fit a b-spline model, where I create a recursive function `basis`, however it doesn’t seem to be able to find itself or, potentially, go deep enough to reach the base case (degree=0) and solve the recursion. This function is called by another user-defined function but I don’t think that’s the problem. My function and the associated error message is below.

``````functions {
vector basis(vector x, int degree, int i, vector knots) {
int N;
vector[N] OUT1;
vector[N] OUT2;
vector[N] alpha1;
vector[N] alpha2;
vector[N] B;
N = num_elements(x);
if(degree==0){
for(j in 1:N){
if((x[j]>=knots[i]) && (x[j] < knots[i+1])){
B[j] = 1;
}else{
B[j] = 0;
}
}
}else{
if((knots[degree+i] - knots[i])>0){
for(j in 1:N){
alpha1[j] = (x[j] - knots[i])/(knots[degree+i] - knots[i]);
}
}else{
alpha1 = rep_vector(0,N);
}
if((knots[i+degree+1] - knots[i+1])>0){
for(j in 1:N){
alpha2[j] = (knots[i+degree+1] - x[j])/(knots[i+degree+1] - knots[i+1]);
}
}else{
alpha2 = rep_vector(0,N);
}
OUT1 = basis(x,degree-1,i,knots);
OUT2 = basis(x,degree-1,(i+1),knots);
for(j in 1:N){
B[j] = alpha1[j]*OUT1[j]+alpha2[j]*OUT2[j];
}
}
return B;
}
...
}
``````

the error message:

``````SYNTAX ERROR, MESSAGE(S) FROM PARSER:
No matches for:

basis(vector, int, int, vector)

error in '...' at line 33, column 38
-------------------------------------------------
31:         alpha2 = rep_vector(0,N);
32:       }
33:       OUT1 = basis(x,degree-1,i,knots);
^
34:       OUT2 = basis(x,degree-1,(i+1),knots);
-------------------------------------------------
``````

Can you try the rstan preview:

``````remove.packages(c("StanHeaders", "rstan"))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
``````

Your model compiles fine for me with that

@andrjohns Thanks for your time. I personally can’t do it, but I’ll ask my admin to help. Do you know why that solves the problem?

The preview contains a newer version of the Stan model parser, which appears to more compatible with recursive functions

On older versions it should be possible to declare a recursive function using a C-style forward declaration like

``````
vector basis(vector x, int degree, int i, vector knots);
``````

Before the definition