In what way does $x_values = $x_values . $x (or indeed the shorter $x_values .= $x) not work?
If the issue is that you’re only returning one value from the function, which means that anything you do to $x and $y inside the function are lost, don’t forget you can return an array or an object, which can contain multiple values.
It might be better if you could describe the actual problem you’re trying to solve, what needs to happen and why it needs to stop where you want it to. It’s difficult to offer solutions without knowing the problem.