'Swift - "unexpected non-void return value in void function"

I've begun writing a user class that has a method for calculating the user's distance from an object. It looks like this:

class User{
    var searchRadius = Int()
    var favorites : [String] = []
    var photo = String()
    var currentLocation = CLLocation()

    func calculateDistance(location: CLLocation){
        let distance = self.currentLocation.distanceFromLocation(location)*0.000621371
        return distance //Error returns on this line
    }
}

At the line marked above, I get the following error:

(!) Unexpected non-void return value in void function

I've looked elsewhere for a solution, but can't seem to find anything that applies to this instance. I've used the distanceFromLocation code elsewhere, and it's worked okay, so I'm not sure what's different about the usage in this case.



Solution 1:[1]

Your function calculateDistance does not specify a return value. That means it does not return anything.

However, you have a line return distance, which is returning a value.

If you want your function to return a distance, you should declare it like this:

func calculateDistance(location: CLLocation) -> CLLocationDistance
{
  //your code
  return distance
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Duncan C