Voici un exemple qui pourra t'aider, du moins, je l'espère :

CREATE FUNCTION calclength(text) RETURNS int4 AS
'
DECLARE
intext ALIAS FOR $1;
resultat int4;
BEGIN
result := (SELECT...