I had seen some documentation on contributing new functions to Stan, but they seemed more on the development than the modeling side. This appears to be more modeling but still require a little more than just writing the model, but it may be what I am looking for after all. Is there a full example of this in the CmdStan documentation? That would be really helpful for the general audience (in which I include myself). Also, if it is implemented as a Stan/Math header and called via CmdStan it means it should be available to any of the interfaces, is that right?

If I manage maybe I can try to write a tutorial for it myself. Thanks.

# Use Stan easily from c++

**twistedmersenne**#21

**bgoodri**#22

Section 4.4

No, there is an additional step where you have to modify the Stan library so that the new function is exposed in the Stan language.

**twistedmersenne**#24

Thanks for the pointers. Iāll give it a go. I think this can be a powerful way of extending Stan, but I think between the CmdStan reference and online wiki the documentation is probably lacking for non-developers, so at this point I still find it easier to implement a simpler version of the model and sampler from scratch than implementing a new Stan function.

The thread mentioned above is helpful, too. I understand the basic usage of templates, but I normally donāt use them and I donāt understand why they are needed here, so all in all Iām still at a loss here. Iāll write an example function and maybe open a new thread for that (or maybe extend that one mentioned). Thanks.

**sakrejda**#25

They are needed because if you provide them then the Stan math library automatically calculates the gradient of the function for you, which is required for HMC, ADVI, and L/BFGS.

**Bob_Carpenter**#26

Thatās an understatement. Weāre working on developer-facing doc. Itās always the last to get attention. The cobblerās children have no shoes and all that.

One way to see how to write a templated function for Stan in full generality is to look at the C++ generated by Stan for a user-defined function. Usually, you wonāt need to template that generally for ordinary use because youāll know the types.