I've had this a few times in the past, but its been forgotten up until now.
With this :
SELECT afunction(value) ...
I have found that, despite returning a valid and explicit value for the CASE values, none of them are actually called.
However, doing :
b1%=afunction(value)
SELECT b1%
solves the problem...
afunction returns an integer.
Oh dear. Filed a bug.