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

iOS Functions in Swift Adding Power to Functions Returning Complex Values

Functions in Swift

I have looked everywhere else for the answer, but can not find it.

functions.swift
// Enter your code below

I'm not going to simply give you the answer yet, but a hint to get started ... Try using a switch statement within the function to specify the coordinates for each location as a case.

1 Answer

Kyle Johnson
Kyle Johnson
33,528 Points

This Code Challenge is asking us to create a function that will return two(2) Doubles (the lat and lon).

To create the function we use the func keyword followed by the name of the function, coordinates. The single parameter is of type String with the external name for and local name location. This is how to correctly write this: (for location: String). To return two(2) Doubles, we must use -> followed by what type we are returning. In this case (Double, Double), two(2) Doubles.

Here is what we have so far.

func coordinates(for location: String) -> (Double, Double) {

}

We can use a switch to assign the names of the locations to their own case and return the lat and lon values. The return should look the same as (Double, Double) but with actual double numbers.

Here's what the switch looks like.

switch location {
    case "Eiffel Tower": 
      return (48.8582, 2.2945)
    case "Great Pyramid": 
      return (29.9792, 31.1344)
    case "Sydney Opera House": 
      return (33.8587, 151.2140)
    default: 
      return (0.0, 0.0)
  }

Finally, added together!

// Enter your code below
func coordinates(for location: String) -> (Double, Double) {
  switch location {
    case "Eiffel Tower": 
      return (48.8582, 2.2945)
    case "Great Pyramid": 
      return (29.9792, 31.1344)
    case "Sydney Opera House": 
      return (33.8587, 151.2140)
    default: 
      return (0.0, 0.0)
  }
}

Kyle, (nice name by the way) Instead of just posting the code for the answer to a code challenge, we try to encourage answers to questions in the Community include an explanation to go along with the code in order to facilitate understanding of the concepts behind the code challenge. Do you think you could edit your answer to include a brief run-down of your thought process for how you answered the challenge?