Welcome to the forum!
Your code does not guarantee that TimeA, DistanceA, etc. are all meaningfully defined, before speed is calculated, so most of the time the function will return nonsense.
Think about what happens if both if conditions are false.
Please, ALWAYS post all of your code. We often need to see how things are defined in order to find mistakes.