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

Sahan Balasuriya
Sahan Balasuriya
10,115 Points

why is it return(price, color) instead of return (price, carpetColor) I don't understand

func carpetCostHaving(length: Int, width: Int, carpetColor color: String = "tan") -> (price: Int, carpetColor: String) {
    //gray carpet - $1 per sq ft
    //tan carpet - $2 per sq ft
    //deep blue carpet - $4 per sq ft

    let areaOfRoom = area(length: length, width: width)

    var price = 0

    switch color {
    case "gray": price = areaOfRoom * 1
    case "tan": price = areaOfRoom * 2
    case "blue": price = areaOfRoom * 4
    default:
        price = 0
    }

    return(price,color)
}

let result = carpetCostHaving(length: 4, width: 12, carpetColor: "gray")

1 Answer

You're using color in switch and returning color because you've defined a local argument name and an external argument name by listing two words with a space in between before defining the argument type.

func carpetCostHaving(..., carpetColor color: String = "tan") -> ...
//                         ↑           ↑__ local name
//                         |__ external name

You begin by defining your external argument name, followed by a space and then a local name. Within the function you use the local name anytime you reference that argument, that's why you're using just color within the function.

This allows you to be a little more flexible with the naming conventions within the scope of the function. If your function is specifically about carpets then it doesn't need you to prefix every variable with carpet, it's contextually implicit that color can only be about carpet color.

Outside of the function, color isn't very descriptive in the wider context where any number of things could all have colors (paint, floor tiles, etc.) so we're being more explicit about the name we use to pass carpet color into the function.