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

Anatomy of Swift Functions

Let’s start with a simple “Hello, World!” Swift function:

 func mySimpleFunction() {
  println("hello, world!")
 }

If you’ve ever programmed in any other language aside from Objective-C, the preceding function should look very familiar:

  • The func keyword denotes that this is a function.
  • The name of this function is mySimpleFunction.
  • There are no parameters passed in—hence, the empty ( ).
  • There is no return value.
  • The function execution happens between the { }.

Now on to a slightly more complex function:

 func myFunctionName(param1: String, param2: Int) -> String {
  return "hello, world!"
 }

This function takes in one parameter named param1 of type String and one parameter named param2 of type Int and returns a String ...

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