Nested Functions
And in case you haven’t had enough of functions from this chapter, it’s always good to know that in Swift, you can have a function inside a function:
| func myFunctionWithNumber(someNumber: Int) { |
| |
| func increment(var someNumber: Int) -> Int { |
| return someNumber + 10 |
| } |
| |
| let incrementedNumber = increment(someNumber) |
| println("The incremented number is \(incrementedNumber)") |
| } |
| |
| myFunctionWithNumber(5) |
| // The incremented number is 15 |
| @end |
Swift functions have a lot of options and a lot of power. As you start writing in Swift, remember: with great power comes great responsibility. Optimize for readability over cleverness!
Swift best practices haven’t been fully established yet, and the language is still constantly ...
Get Functional Programming: A PragPub Anthology 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.