Return the id after insert or select
You need to store the value that you want to return somewhere:

CREATE OR REPLACE FUNCTION f_get(ikey text)
  returns integer
  AS
$func$
DECLARE 
  l_id integer;
BEGIN
  LOCK TABLE foo IN SHARE ROW EXCLUSIVE MODE;
  INSERT INTO foo (type)
  SELECT ikey
   WHERE NOT EXISTS (
     SELECT * FROM foo WHERE type=ikey
   )
   returning id into l_id; --&lt; store the returned ID in local variable
   return l_id; --&lt; return this variable
END
$func$ LANGUAGE plpgsql;


You could have done that with the original function and an OUT parameter as well. In that case you just need to use returning id into out_key;