Preparing a function with a loop