Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Kamran Ismayilov
Kamran Ismayilov
5,753 Points

return does not return anything?

can you please explain why those methods bellow do not return any value but works fine?

if error.code == CLError.LocationUnknown.rawValue { return }

if (authStatus == .Denied || authStatus == .Restricted) { showLocationServicesDeniedAlert() return }

    if authStatus == .NotDetermined {

1 Answer

Matthew Young
Matthew Young
5,133 Points

Even if a function doesn't have an explicit return type, functions will have a return type of Void. As stated in The Swift Programming Language guide from Apple:

"Functions without a defined return type return a special value of type Void. This is simply an empty tuple, in effect a tuple with zero elements, which can be written as ()."

In other words, this is just the default behavior for a function. Even if you don't want anything returned, at the end of a non-returning function there's an implicit return of void. Therefore, your code will function even if you return nothing. However, as with executing any other typical return statement in a function, you will break out of your function's closure and cease executing any remaining code within the closure.

Assuming your code provided is found within a completionHandler for a data task, the reason why you're adding "return" without returning anything in this case is that it stops executing the remaining code in your completionHandler. This is important if certain conditions aren't met in a network request such as data not being returned or if the data received is not what you want to have processed.

For example, if your authorization status is either denied or restricted (referring to the second "if" statement in your code), you won't have access to the data you're requesting so it's best to break out of your completionHandler by executing the "return" statement.

Source: The Swift Programming Language - Functions - Functions Without Return Values