return duration; // Returns the duration of the pulse
In what units? Counting as fast as the processor can until the return echo is received provides you with no clue as to how long the pulse took to arrive, or how far the pulse had to travel.
As a result, you have a useless, dimensionless value.
Why you are (incorrectly) trying to replicate the pulseIn() method, which provides a dimensioned value, escapes me.
The comments in the code are wrong, anyway. There are no pulses being sent/timed.