Staying in a subroutine

Sure. Use a "for" loop to drive the function and code the for loop to watch a boolean to change from true to false. Somewhere in your function, change the boolean to false and the loop will end.
Paul

Better yet, use a "while" loop.