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 Default Values

I'm having trouble following with the Default Values video I'm getting an error in Xcode

//Dafault Values

func carpetCostHaving(length: Int, width: Int, carpetColor color: String = "tan") -> Int { //Gray carpet - $1/sq ft //tan carpet - $2/sq ft //Deep Blue Carpet - $4/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 * 3
default: price = 0
}
return price 

}

1 Answer

Thomas Dobson
Thomas Dobson
7,511 Points

Hi Jacob,

your calling the area function but I dont see it defined in your code.

If memory serves me it should look something like this:

//Dafault Values

func area(length: Int, width: Int) -> Int {
    return length * width
}

func carpetCostHaving(length: Int, width: Int, carpetColor color: String = "tan") -> Int { //Gray carpet - $1/sq ft //tan carpet - $2/sq ft //Deep Blue Carpet - $4/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 * 3
    default: price = 0
    }
    return price
}

carpetCostHaving(length: 10, width: 15, carpetColor: "tan")
// returns 300

I hope this helps