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