/*
함수 : 특정 작업을 수행하는 독립적인 코드 덩어리
예)
커피 내리기
1. 물을 끓인다.
2. 커피 공을 간다.
3. 필터에 넣고 물을 흘려보낸다.
4. 커피를 따뜻하게 유지시킨다.
...
DRY : Don't Repeat Yourself
*/
// Wall #1
let width = 25
let height = 7
let area = width * height
// Wall #2
let secondWidth = 14
let secondHeight = 3
let secondArea = secondWidth * secondHeight
func calculateArea() {
let width = 25
let height = 7
let area = width * height
print(area)
}
calculateArea() // 175
func calculateArea2(width: Int) {
let height = 7
let area = width * height
print(area)
}
calculateArea2(width: 10) // 70
calculateArea2(width: 20) // 140
func calculateArea3(width: Int, height: Int ) {
let area = width * height
print(area)
}
// Wall #3
calculateArea3(width: 17, height: 10) // 170
// Wall #4
calculateArea3(width: 14, height: 3) // 42
func calculaterArea4(width: Int ,height: Int ) -> Int {
let area = width * height
return area
}
let areaOfWall1 = calculaterArea4(width: 17, height: 10)
// Hint : Double
// C = (F-32) * 5/9
func convertToCelsius (temperature: Double ) -> Double {
let temperatureInCelsius = (temperature - 32) * 5/9
return temperatureInCelsius
}
let C = convertToCelsius(temperature: 80)
print(C) // 26.6666666666667
func convertToCelsius2 (temperatureInFahrenheit temp: Double ) -> Double {
let temperatureInCelsius = (temp - 32) * 5/9
return temperatureInCelsius
}
convertToCelsius2(temperatureInFahrenheit: 80)
func sayHello(to person: String, and anotherPerson: String) -> String {
return "Hello \(person) and \(anotherPerson)"
}
sayHello(to: "Jack", and: "Jill")
let sayHello1 = sayHello(to: "Jack", and: "Jill")
print(sayHello1) // "Hello Jack and Jill\n"
func calculateWallpaperPrice(area: Int , color: String) -> Int {
var price: Int
switch color {
case "beige": price = area * 10000
case "gray": price = area * 15000
case "pink": price = area * 20000
default: price = 0
}
return price
}
// 함수 areaOfWall1 를 호출해서 area에 넣어준다.
calculateWallpaperPrice(area: areaOfWall1, color: "beige")
func calculateWallpaperPrice2 (width: Int ,height: Int , color: String) -> Int {
var price: Int
let area = calculaterArea4(width: width, height: height)
switch color {
case "beige": price = area * 10000
case "gray": price = area * 15000
case "pink": price = area * 20000
default: price = 0
}
return price
}
calculateWallpaperPrice2(width: 17, height: 10, color: "beige")
func calculateWallpaperPrice3 (width: Int ,height: Int , color: String = "beige" ) -> Int {
var price: Int
let area = calculaterArea4(width: width, height: height)
switch color {
case "beige": price = area * 10000
case "gray": price = area * 15000
case "pink": price = area * 20000
default: price = 0
}
return price
}
// 함수의 파라미터 기본값을 설정하면 아래와 같이 2개 값만 입력 가능하게 나온다.
//calculateWallpaperPrice3(width: <#T##Int#>, height: <#T##Int#>)
//calculateWallpaperPrice3(width: <#T##Int#>, height: <#T##Int#>, color: <#T##String#>)
calculateWallpaperPrice3(width: 15, height: 10) // 1500000
func calculateWallpaperPrice4 (width: Int ,height: Int , color: String = "beige" ) -> (Int, String) {
var price: Int
let area = calculaterArea4(width: width, height: height)
switch color {
case "beige": price = area * 10000
case "gray": price = area * 15000
case "pink": price = area * 20000
default: price = 0
}
return (price,color)
}
let result = calculateWallpaperPrice4(width: 14, height: 13) // (.0 1820000, .1 "beige")
print(result) // "(1820000, "beige")\n"
print(result.0) // "1820000\n"
print(result.1) // "beige\n"
func calculateWallpaperPrice5 (width: Int ,height: Int , color: String = "beige" ) -> (price: Int, color: String) {
var price: Int
let area = calculaterArea4(width: width, height: height)
switch color {
case "beige": price = area * 10000
case "gray": price = area * 15000
case "pink": price = area * 20000
default: price = 0
}
return (price,color)
}
let result2 = calculateWallpaperPrice5(width: 12, height: 11) // (price 1320000, color "beige")
print(result2.price) // "1320000\n"
print(result2.color) // "beige\n"
// 함수 스코프 scope
func incrementAndDecrement(a: Int) {
var a = a
var b = a
a += 1
b -= 1
}
let a = 20
incrementAndDecrement(a: 20)
a // 20
'Swift > 기초&문법' 카테고리의 다른 글
swift struct class 스위프트 구조체 클래스 value type reference type (0) | 2018.10.13 |
---|---|
스위프트 할일 앱 노티피케이션 swift Todos nofitication (0) | 2018.10.12 |
스위프트 고차함수 swift higher-order function (0) | 2018.10.10 |
스위프트 오류처리 swift Error (0) | 2018.10.09 |
스위프트 성격유형검사 앱 swift DISCTest (0) | 2018.10.08 |