The custom field type parameter should be dynamic my code looks something like the following:
create or replace function somefunc(ip_address varchar, ip_type varchar)
returns table(fielda varchar,
fieldb varchar,
language plpgsql
as $$
begin
return query
execute format('select fielda,
fieldb,
from
ips
join
names on id = other_id
where
iprange >>= ip_address::%s', ip_type);
end;
$$
As you can see I've tried using format, but then it thinks ip_address is a column.