Define custom functions once per kernel

This also allows to use more natural user-provided function names and
increases the overall readability of the generated kenels.