This is an old version, view current version.

9.9 Declarations

In general, functions do not need to be declared before they are used. Despite this, Stan supports forward declarations, which look like function definitions without bodies. For example,

real unit_normal_lpdf(real y);

declares a function named unit_normal_lpdf that consumes a single real-valued input and produces a real-valued output. A function definition with a body simultaneously declares and defines the named function, as in

real unit_normal_lpdf(real y) {
  return -0.5 * square(y);
}

A user-defined Stan function may be declared and then later defined, or just defined without being declared. No other combination of declaration and definition is legal, so that, for instance, a function may not be declared more than once, nor may it be defined more than once. However, functions with different argument types are considered distinct even if they have the same name; see the section on function overloading. If there is a declaration, there must be a definition. These rules together ensure that all the declared functions are eventually defined.