func greet(person: String) -> String { let greeting = "Hello, " + person + "!" return greeting }
print(greet(person: "Anna"))
print(greet(person: "Brian"))
func greetAgain(person: String) -> String { return "Hello again, " + person + "!" } print(greetAgain(person: "Anna"))
func sayHelloWorld() -> String { return "hello, world" } print(sayHelloWorld())
func greet(person: String, alreadyGreeted: Bool) -> String { if alreadyGreeted { return greetAgain(person: person) } else { return greet(person: person) } } print(greet(person: "Tim", alreadyGreeted: true))
func greet(person: String) { print("Hello, \(person)!") } greet(person: "Dave")
func minMax(array: [Int]) -> (min: Int, max: Int) { var currentMin = array[0] var currentMax = array[0] for value in array[1..<array.count] { if value < currentMin { currentMin = value } else if value > currentMax { currentMax = value } } return (currentMin, currentMax) }
let bounds = minMax(array: [8, -6, 2, 109, 3, 71]) print("min is \(bounds.min) and max is \(bounds.max)")
func minMax(array: [Int]) -> (min: Int, max: Int)? { if array.isEmpty { return nil } var currentMin = array[0] var currentMax = array[0] for value in array[1..<array.count] { if value < currentMin { currentMin = value } else if value > currentMax { currentMax = value } } return (currentMin, currentMax) }
if let bounds = minMax(array: [8, -6, 2, 109, 3, 71]) { print("min is \(bounds.min) and max is \(bounds.max)") }
func someFunction(firstParameterName: Int, secondParameterName: Int) { } someFunction(firstParameterName: 1, secondParameterName: 2)
func someFunction(argumentLabel parameterName: Int) { }
func greet(person: String, from hometown: String) -> String { return "Hello \(person)! Glad you could visit from \(hometown)." } print(greet(person: "Bill", from: "Cupertino"))
func someFunction(_ firstParameterName: Int, secondParameterName: Int) { } someFunction(1, secondParameterName: 2)
func someFunction(parameterWithoutDefault: Int, parameterWithDefault: Int = 12) { } someFunction(parameterWithoutDefault: 3, parameterWithDefault: 6) someFunction(parameterWithoutDefault: 4)
|