Get multiple columns from a function depending on outer group by
You want to do a LATERAL join, by putting the the function in the FROM list.  If you just put the function in the from list after table "a", you don't need the LATERAL keyword, as joins to functions are automatically lateral.  If you wrap it in a dummy "select * from", then you do need the keyword:

WITH A AS (SELECT DISTINCT trip_id FROM stop_times)
SELECT route.*
FROM A CROSS JOIN LATERAL (SELECT * FROM getroutepertrip(trip_id)) as route 
GROUP BY trip_id
ORDER by trip_id


or

WITH A AS (SELECT DISTINCT trip_id FROM stop_times)
SELECT route.*
FROM A, getroutepertrip(trip_id) as route 
GROUP BY trip_id
ORDER by trip_id