Function Types in Swift

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.

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:

The type of this function is () -> (), 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.

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:

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

As with any other type, you can leave it to Swift to infer the function type when you assign a function to a constant or variable:

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:

This example defines a function called printMathResult, which has three parameters.

  1. 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.
  2. The second and third parameters are called a and b, and are both of typeInt.
  3. 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.

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.

The example determines whether a increase or decrease a variable called currentValue progressively. currentValue has an initial value of 3, which means thatcurrentValue > 0 returns true, causing chooseStepFunction to return the increase function. A reference to the returned function is stored in a constant called moveNearerToZero.