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!

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


Parameter label not specified but required and raw value calculation is incorrect?

Why won't my code work without specifying the "to" label when I call the function distance even though I didn't specify that label? Also, I added a raw value for each planet equivalent to their distance from the the sun but when I subtract two planets from one another it doesn't return correct distance between them. Is my calculation incorrect? Thank you in advance!

import UIKit

enum planet : Double {
    case Sun = 0, // The star of the solar system is set to 0
    Venus = 67.24,
    Earth = 92.96,
    Mars = 141.6,
    Jupiter = 483.8,
    Saturn = 890.7,
    Uranus = 1787,
    Neptune = 2795

// I didn't specify a label for to here:
func distance(from: planet, to: planet)-> Double {
    return abs(from.rawValue - to.rawValue)

// But Xcode won't compile the code if I don't add the to label:
distance(.Earth, to: .Mars)
// Returns 48.64 instead of 54.6

1 Answer

Nathan Tallack
Nathan Tallack
22,159 Points

By default the first parameter does not require its label, but all subsequent ones do. So you always have to label your non-first parameters when passing values into a function/method.

You have the ability to have an external parameter name for any of your parameters. If you do that on the first one, then you also must use it when you are passing parameters into the function/method. Consider this example code below.

func distance(fromPlanet from: planet, toPlanet to: planet)-> Double {
    return abs(from.rawValue - to.rawValue)

distance(fromPlanet: .Earth, toPlanet: .Mars)

Now with external parameter names you can chose to omit the external parameter name by using an underscore. In this way you could achieve what you wanted to do. Consider this example code below.

func distance(from: planet, _ to: planet)-> Double {
    return abs(from.rawValue - to.rawValue)

distance(.Earth, .Mars)

Note that we did not specify an external parameter name for the first parameter in this case because without it it is automatically not required.

On a side note, it is custom to name your enum's with a capital to show that they are an Object rather than a value. So you should name your enum Planet not planet. ;)

Thank you so much for taking the time to write this answer.