drmstfky
8/28/2018 - 3:21 PM

Swift Functions

// Area calculation for room #2

let secondLength = 14
let secondWidth = 8

let secondArea = secondLength * secondWidth

// Area calculation for room # 1

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

let areaOfFirstRoom = area(length: 10, width: 12)
let areaOfSecondRoom = area(length: 15, width: 22)

// Same Three
// func someFunction() -> Void {}
// func someFunction() -> () {}
// func someFunction() {}

// Naming Conventions for Function Name
func move(toX: Int) {}


// func area(withLength: Int, width: Int) {} <-- WRONG USAGE
// func area(withLenght: Int, withWidth: Int) {} <-- WRONG USAGE
// func areaWith(length: Int, width: Int) {} <-- CORRECT WAY

// Argument Labels

func remove(havingValue value: String) {
    print(value)
}

remove(havingValue: "Mustafa")

// Default Values

func carpetCostHaving(length: Int, width: Int, carpetColor color: String = "tan") -> (price: Int, carpetColor: String) {
    // 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 * 4
    default: price = 0
    }
    
    return (price, color)
}
carpetCostHaving(length: 10, width: 20, carpetColor: "blue")
let result = carpetCostHaving(length: 10, width: 20)
result.price
result.carpetColor

// Scope

func arrayModifier(array: [Int]) {
    var arrayOfInts = array
    arrayOfInts.append(5)
    
    var secondArray = arrayOfInts
}


var arrayOfInts = [1,2,3,4]
arrayModifier(array: arrayOfInts)

arrayOfInts

// No Label like append function
func someFunction(_ test: Int) {}

someFunction(10)


func coordinates(for location: String) -> (Double, Double) {
    
    var tuple: (Double, Double) = (0, 0)
    switch location {
    case "Eiffel Tower": tuple = (48.8582, 2.2945)
    case "Great Pyramid": tuple = (29.9792, 31.1344)
    case "Sydney Opera House": tuple = (33.8587, 151.2140)
    default: tuple = (0, 0)
    }
    return tuple
}