Return Types

Next you’ll create a function that returns its sum, which will also be of return type Int:

func sum(a: Int, b: Int) -> Int {  return a + b}

This declaration can be read as “a function, sum, that takes two arguments of type Int and has a return value of type Int.” If you wanted to call the new function, it could look something like this:

let total = sum(14, 52)// total = 66

Returning a single value is just fine, but sometimes you want to return multiple values. In Objective-C, this problem is usually solved by creating an object class or by returning some sort of collection. These solutions would work in Swift as well, but there is a better way: You can return multiple arguments in Swift by using tuples, ...

Get Learning Swift™ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.