Introduction to Swift: Functions

In Swift, functions are a self contain block of code which targets a specific task. Functions are first-class types, which means functions can be defined outside of a class in Swift. To follow this tutorial you will need Xcode 6 and start a playground file. Xcode 6 can be downloaded from Apple's developer portal.

Declare Functions

A function start with the keyword func, has zero or more parameters and a return type. The general syntax is 

func functionname(parameter1:type, ...) -> returnType 
{
  return expression
}

For example, let's create a function which multiply two numbers

func multiplyNumbers(number1:Int, number2:Int) -> Int 
{
  return number1 * number2
}

This function can be called and its return value can be assigned to an Integer

var area = multiplyNumbers(3,6)
// area = 18

Functions without Parameters

A function doesn't have to contain parameters, Say, for example, a function only need to return some String which can be printed

func helloSwift() -> String 
{
    return "Hello Swift"
}

// called with
println(helloSwift())

Functions without Return Type

There can be also functions  which don't have a return type. See the following example.

func greetPerson(name: String)
{
  println("Hello, \(name)")
}

// called with
greetPerson("Jim")

Functions with multiple return values

In Swift, you can use a tuple type as the return type for a function to return multiple values as part of one compound return value.

func calculation(number1: Int, number2:Int) -> (sum:Int, multiply:Int)
{
  let sum = number1 + number2
  let multiply = number1 * number2
       
  return (sum,multiply)
}   
   
// called with
let number1 = 3
let number2 = 4
let math = calculation(number1,number2)

println("The sum of \(number1) + \(number2) = \(math.sum)")
// The sum of 3 + 4 = 7
println("The multiplication of \(number1) * \(number2) = \(math.multiply)")
// The multiplication of 3 * 4 = 12

External Parameter names

External parameter names are useful to name each parameter when you call a function.
In addition to the local parameter name you can define an external parameter name. The external parameter name must be written before the local name. If you use an external parameter name, you should always use it when you call the function, otherwise you will get an compiler error. 

class Rectangle {
  var height:Int = 0
  var width:Int = 0

  func resize(width w:Int, height h:Int) {
    self.width = w
    self.height = h
  }
}

//called with
var rect = Rectangle()
rect.resize(width:5, height:10)

Shorthand External Parameter Names

If the internal name is descriptive enough and can be used as the external name, then you can use implicit external naming

class Rectangle {
  var width: Int = 0
  var height: Int = 0

  func resize(#width:Int, #height:Int) 
  {
    self.width = width
    self.height = height
  }   
}   
 
// call with
var rect = Rectangle()
rect.resize(width:5, height:10)

This finished this part of the introduction serie of Swift programming.