"Put the function at the end of your file for example and call it inside the loop." . . . .