><>, 1717 16 bytes
vn;1v;n
$<*}-1
>$.!?::?!.1-}*
-1 byte thanks to Jo King.
Since the question asks for a function as opposed to a full program, I allowed myself to accept the input from the stack without counting an additional 3 bytes for using the -v
option.
This manages to be shorter than the other ><> answer because it jumps to the end-of-iteration code without having to hardcode the jump destination address : the current iteration counter (duplicated) is used as an address.
The iteration stops when the counter is 0, and jumping to (0, 0)
while the direction pointer points to the right will execute the n;
code that is otherwise unreachable, displaying the result and stopping the execution.
It handles 0!
correctly and executes in 10*(n+1) ticks for n > 0 or 9 ticks for n = 0.
You can try it online.