Function to compute difference between 2 times
Assuming exp_hr is always ahead in time, but never more than 24 hours.
I suggest to return an interval for simplicity. Then it works for hours, minutes, seconds, microseconds etc. alike.
Besides correct, this should be faster by orders of magnitude:
CREATE OR REPLACE FUNCTION f_time_diff(hr INT, min INT, sec INT, exp_hr INT)
  RETURNS interval
  LANGUAGE sql PARALLEL SAFE IMMUTABLE AS
$func$
SELECT CASE WHEN hr &gt;= exp_hr       -- wrap around midnight
            THEN 24 + exp_hr - hr
            ELSE exp_hr - hr
            END * interval '1 hour'
                - interval '1 min' * min
                - interval '1 sec' * sec;
$func$;

Note that this returns 24h for input = output exactly. You might decide otherwise for the corner case.
Demo:
test=*&gt; SELECT f_time_diff( 1, 15, 0, 2) AS example1
test-*&gt;      , f_time_diff(23, 15, 0, 1) AS example2
test-*&gt;      , f_time_diff( 1, 15, 0, 1) AS example3
test-*&gt;      , f_time_diff( 1,  0, 0, 1) AS example4;
 example1 | example2 | example3 | example4 
----------+----------+----------+----------
 00:45:00 | 01:45:00 | 23:45:00 | 24:00:00
(1 row)

If you actually need the number of minutes as integer, (truncating seconds if any), extract the epoch and use integer division:
SELECT EXTRACT(epoch FROM f_time_diff(23,15,0,1))::int / 60 AS example2;  -- 105

(Or build that into the function.)
db&lt;&gt;fiddle here
The most expensive part is the type conversion now. If you input time / interval, it gets much faster, yet. Or better yet: timestamp or timestamptz, then it gets trivial and you wouldn't need a function to begin with ...
Concatenating strings before casting to time (like you had it) is much more expensive, yet.