Till now we have understood the how a function is defined and how can we define the parameters in function definition. Lets recap the functions anatomy.

1 2 3 |
func addTwoInts(a: Int, b: Int) -> Int { return a + b } |

Every function has a specific *function type*, made up of the parameter types and the return type of the function. This function take two `Int`

values, and return an `Int`

value, which is the result of performing an appropriate mathematical operation. The type of this functions is `(Int, Int) -> Int`

. This can be read as:

“A function type that has two parameters, both of type `Int`

, and that returns a value of type `Int`

.”

Here’s another example, for a function with no parameters or return value:

1 2 3 |
func printHelloFolks() { println("hello,Folks") } |

`() -> ()`

, or “a function that has no parameters, and returns `Void`

.
In swift we can use function as a type while assigning just like any other types in Swift.

For example, you can define a constant or variable to be of a function type and assign an appropriate function to that variable: consider the following example.

1 |
var mathFunction: (Int, Int) -> Int = addTwoInts |

This can be read as:

“Define a variable called `mathFunction`

, which has a type of ‘a function that takes two `Int`

values, and returns an `Int`

value.’ Set this new variable to refer to the function called `addTwoInts`

.”

You can now call the assigned function with the name `mathFunction`

:

1 2 |
println("Result: \(mathFunction(2, 3))") // prints "Result: 5" |

A different function with the same matching type can be assigned to the same variable, in the same way.

1 2 3 |
func multiplyTwoInts(a: Int, b: Int) -> Int { return a * b } |

1 2 3 |
mathFunction = multiplyTwoInts println("Result: \(mathFunction(2, 3))") // prints "Result: 6" |

1 2 |
let simpleMathFunction = addTwoInts // simpleMathFunction is inferred to be of type (Int, Int) -> Int |

### Function as a Parameter Type:

You can use a function type such as `(Int, Int) -> Int`

as a parameter type for another function. Here’s an example to print the results of the math functions from above:

1 2 3 4 5 |
func printMathResult(mathFunction: (Int, Int) -> Int, a: Int, b: Int) { println("Result: \(mathFunction(a, b))") } printMathResult(addTwoInts, 3, 5) // prints "Result: 8" |

This example defines a function called `printMathResult`

, which has three parameters.

- The first parameter is called
`mathFunction`

, and is of type`(Int, Int) -> Int`

. You can pass any function of that type as the argument for this first parameter. - The second and third parameters are called
`a`

and`b`

, and are both of type`Int`

. - These are used as the two input values for the provided math function.

When `printMathResult`

is called, it is passed the `addTwoInts`

function, and the integer values `3`

and `5`

. It calls the provided function with the values `3`

and `5`

, and prints the result of `8`

.

### Function as a Return Type:

You can use a function type as the return type of another function.

1 2 3 4 5 6 |
func increases(input: Int) -> Int { return input + 1 } func decrease(input: Int) -> Int { return input - 1 } |

Here we have defined two functions named increase and decrease with (Int) -> Int as he function type. now lets define another function. This function will have a function as its return type i.e it will return a function call to increase(Int)->Int or decrease(Int)->Int as its return type.

1 2 3 4 5 6 7 8 |
func chooseStepFunction(backwards: Bool) -> (Int) -> Int { return backwards ? increase : decrease } var currentValue = 3 let moveNearerToZero = chooseStepFunction(currentValue > 0) // moveNearerToZero now refers to the stepBackward() function |

The example determines whether a increase or decrease a variable called `currentValue`

progressively. `currentValue`

has an initial value of `3`

, which means that`currentValue > 0`

returns `true`

, causing `chooseStepFunction`

to return the increase function. A reference to the returned function is stored in a constant called `moveNearerToZero`

.