Returning Values

suggest change

Functions 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))
}

let c = findHypotenuse(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(5)
//resultTuple = (10, 15)

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents