Functions
A function takes inputs and gives outputs. In Swift, we must tell the compiler the types of our inputs and the type of our output.
func sayHello(to name: String) -> String {
return "Hello, \(name)!"
}
sayHello(to: "Mr.Whiskers")
sayHello(to: "Tunses")
sayHello(to: "Kitty")
sayHello
took a String
and returned a String
.
func addTwoNumbers(_ a: Int, _ b: Int) -> Int {
return a + b
}
addTwoNumbers(10, 20)
If you add a _
before the parameter names you can exclude them when you call the function.
A function takes inputs and gives outputs. In Swift, we must tell the compiler the types of our inputs and the type of our output.
func sayHello(to name: String) -> String {
return "Hello, \(name)!"
}
sayHello(to: "Mr.Whiskers")
sayHello(to: "Tunses")
sayHello(to: "Kitty")
sayHello
took a String
and returned a String
.
func addTwoNumbers(_ a: Int, _ b: Int) -> Int {
return a + b
}
addTwoNumbers(10, 20)
If you add a _
before the parameter names you can exclude them when you call the function.
Void Functions
func foo() -> Void {
print("Hello, World!")
}
func bar() {
print("Hello, World!")
}
foo()
bar()
These functions don’t return any data. Such functions are said to return Void
.
Note that you do not have to write -> Void
always, such as with bar
.
One-line Functions
func diceRoll() -> Int {
Int.random(in: 1...6)
}
diceRoll()
Functions that are one line can omit the return
keyword.
Testing Functions
func square(_ x: Int) -> Int {
return x * x
}
square(3) == 9 // true
square(2) == 5 // false
let result: Bool = square(2) == 4
assert(square(3) == 9)
// assert(false) // Uncomment to see a failing assertion
- The expressions
square(3) == 9
andsquare(2) == 5
evaluate to aBool
. - Use
assert
to crash on a false condition, signaling an error.
Exercises
Coffee Shop Order
- Write a functionthat returns
func submitOrder(for customer: String, item: String) -> String
"Order submitted for <customer> for a <item>"
. - Test with:
assert(submitOrder(for: "Kailey", item: "Latte") == "Order submitted for Kailey for a Latte") assert(submitOrder(for: "Alex", item: "Espresso") == "Order submitted for Alex for a Espresso")
- Write a function
Order Total Calculator
- Write a functionthat returns the sum of all prices.
func calculateTotal(prices: [Double]) -> Double
- Test with:
assert(calculateTotal(prices: [3.5, 4.0, 2.75]) == 10.25)
- Write a function
Discount Application
- Write a functionthat returns the total after applying the discount percentage.
func applyDiscount(to total: Double, percent: Double) -> Double
- Test with:
assert(applyDiscount(to: 50, percent: 10) == 45)
- Write a function
Tip Calculator
- Write a functionthat adds a tip (percent) to the total.
func addTip(to total: Double, tipPercent: Double) -> Double
- Test with:
assert(addTip(to: 40, tipPercent: 15) == 46)
- Write a function
Receipt Formatter
- Write a functionthat returns a multi-line receipt with each item on its own line, then
func formatReceipt(items: [String], total: Double) -> String
"Total: $<total>"
. - Test with sample items and total.
- Write a function
Bonus – Process Multiple Orders
- Write a functionthat returns a summary string listing each order on its own line and a final
func processOrders(orders: [(customer: String, item: String, price: Double)]) -> String
"Combined total: $<sum>"
line. - Test with at least three different orders.
- Write a function