need help with subroutine

When posting code, please put code tags around it (press the "#" symbol in the post editor.)

Inside of your function, you redefine the variable "distance" which is also a global variable. I don't think you meant to do that. I think you wanted to modify the global variable "distance" in which cause, you wouldn't redefine it with "float" inside of your function.