Swift - Function Function Types

Introduction

Every function has a specific function type.

To understand this, consider the following two functions:

func sum(num1: Int, num2: Int) -> Int {
   return num1 + num2
}

func diff(num1: Int, num2: Int) -> Int {
   return abs (num1 - num2)
}

Both functions accept two parameters and return a value of type Int. The type of each function is hence (Int, Int) -> Int.

For example, the following function has the type () -> ().

func myFunction() {
    print("myFunction")
}

A function that does not have any parameters and returns Void.

Related Topics