Created
April 29, 2016 14:11
-
-
Save thanhluu/f67878fdff8b293be7714024fe5a983d to your computer and use it in GitHub Desktop.
Swift Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Swift Functions | |
func calculateArea(length: Int, width: Int) -> Int { | |
let area = length * width | |
return area | |
} | |
// Room 1 | |
let areaOfRoom1 = calculateArea(10, width: 8) | |
areaOfRoom1 | |
// Room 1 | |
calculateArea(12, width: 15) | |
func concatenateStrings(a: String, b: String) -> String { | |
return a + b | |
} | |
concatenateStrings("A", b: "B") | |
func sayHello(to person: String, and anotherPerson: String) -> String { | |
return "Hello \(person) and \(anotherPerson)" | |
} | |
sayHello(to: "Pasan", and: "Gabe") | |
// Default Values | |
func carpetCostCalculator( length length:Int, width: Int, carpetColor: String = "tan" ) -> (price: Int, carpetColor: String) { | |
// Gray Carpet - $1/sq ft | |
// Tan Carpet - $2/sq ft | |
// Deep Blue Carpet - $4/sq ft | |
let area = calculateArea(length, width: width) | |
var price: Int | |
switch carpetColor { | |
case "gray": price = area * 1 | |
case "tan": price = area * 2 | |
case "blue": price = area * 4 | |
default: price = 0 | |
} | |
return (price, carpetColor) | |
} | |
let result = carpetCostCalculator(length: 10, width: 20) | |
result.price | |
result.carpetColor | |
result.0 | |
// Function Scope | |
func countDownAndUp(var a: Int) { | |
var b = a | |
while b >= 0 { | |
b-- | |
a++ | |
print("a: \(a)") | |
print("b: \(b)") | |
} | |
} | |
var a = 20 | |
countDownAndUp(a) | |
// Enter your code below | |
func getTowerCoordinates( location: String ) -> ( Double, Double ) { | |
switch location { | |
case "Eiffel Tower": return (48.8582, 2.2945) | |
case "Great Pyramid": return (29.9792, 31.1344) | |
case "Sydney Opera House": return (33.8587, 151.2140) | |
default: return (0, 0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment