Returning Values
suggest changeFunctions can return values by specifying the type after the list of parameters.
func findHypotenuse(a: Double, b: Double) -> Double
{
return sqrt((a * a) + (b * b)) // sqrt is in Darwin
}
// Swift 3 and later: findHypotenuse(a: 3, b: 5). Swift 2 omitted the first label.
let c = findHypotenuse(a: 3, b: 5)
//c = 5.830951894845301
Functions can also return multiple values using tuples.
func maths(number: Int) -> (times2: Int, times3: Int)
{
let two = number * 2
let three = number * 3
return (two, three)
}
let resultTuple = maths(number: 5) // Swift 2: maths(5)
//resultTuple = (10, 15)
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents