What this function returns would be more clear if the return were at the end of the function and the loop would break to allow returning r.