How to get code-snippet to only 'loop' once?

Unikron:
How could I "translate" this to a function?
And call it from the loop?

Have a look at Planning and Implementing a Program in conjunction with the advice given by @ardly

...R