Stan:函数中的局部作用域

Stan: Local scopes in functions

我是 Stan 的新手,已经经历了 manual (version 2.23). What was new to me was that variable hiding is not allowed: you can not use a local variable (e.g. in a for-loop) that has been defined globally (i.e. outside the for-loop) (chapter 7.9, Local Variable Declarations)。

用户自定义函数也是这样吗?即,您可以在用户定义的函数中声明与在函数外部声明的其他变量同名的变量吗?在我们的例子中,我们有

functions{
real[] my_function (x) {
    real init[K*2] = some_declaration_involving_x
    return(some_other_value_involving_init[])
  }
}

transformed data {
  real init[K*6] = some_other_declaration; // initial values
}

transformed parameters {
  yet_another_variable = my_function(some_variable)
}

是的,很容易测试:

functions {
  int fun() {
    int N = 1;
    return N;
  }
}

model {
  real N = 2;
  print(fun(), N);
}