Thanks for your help, I found the add-score function to work in the way I want it to. Never stumbled across it in the documentation, though...
Jan-Peter, I want to add both the markup and the score immediately.
Another question, is it possible to determine inside the function if it is called from a score context, that means to distinguish these two cases:
A:
\score {
\myfunction
}
B:
\myfunction