O'Reilly logo

Functional Programming: A PragPub Anthology by Michael Swaine

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required