iOS Tutorial [SWIFT] – What Are Functions?

ios tutorial swift functions

In this iOS tutorial SWIFT functions, we’re going to show you what functions are and how to use them through some great examples.

Recommended for you The difference between “!” and “?” in Swift


Functions are typically comprised of three things: the name of the function, its parameter(s), and a return type. 

Here’s an example of a function written in Swift:

func yourFuncName (paramName: paramType) ->returnType
    //Do something here

Here is how you would call your function “yourFuncName”:


Now let’s create a legitimate function which returns the sum of two integers:

fun add(x: Int, y:Int) ->Int
    return x + y
let theSum = add(12, y:14)
print ("\(theSum)") //This will print 26

So let’s break down the function above. We gave it a name of “add”. That’s the first component. We then gave it two parameters  called “x” and “y”, and set the parameter types to integers. That covers the second component. For our third and final component of our function, we gave it a return type of “Int”. In the body of our function, we told it to return the value of the sum of our integers. So in our next line of code where we create the constant called “theSum”, we pass in the integers “12” and “14” to our function. Our “add” function sets “x” equal to “12” and “y” equal to “14”. It then goes about it’s arithmetic process and returns the value of “26”, which we printed out in our last line of code.

You might be interested in [SWIFT] Value and Reference Types


We talked about how parameters are one of the key components to a function, so let’s go over different types of parameters.

Default Parameter Values

If a parameter has been declared with a default value, then it will also be an external parameter. External parameters provide better readability when passing in values into a function.

Here is an example of incorporating default parameters in a function:

func countAnimals (numberOfAnimals numAnimals:Int, nameOfAnimal: String = "Dog") ->String
    return String(numAnimals) + nameOfAnimal
let farm = countAnimals(numberOfAnimals:6)

We create a function called “countAnimals” and give it two parameters: The first one of being type int, and the second one being of type String. We want to the function to return type string, so obviously we are going to convert our integer in the function. We did so by doing String(numAnimals). After that we created a constant, and passed in the numberOfAnimals value. Notice how we didn’t have to pass in the nameOfAnimal value. Since we are using a default parameter, we default to “Dog” every single time unless we include that parameter and change it to “Chicken” or “Cow”.

Variadic Parameters

These types of parameters permit you to pass as MANY parameters into a function as you’d like. To implement an unlimited amount of parameters, you need to use an ellipses which is three individual period: (…).

Here’s an example:

fun average(digits digits:Int...) ->
    var total = 0
    for x in digits
        total +=x
    return total /digits.count

The function above will give us an average of all the numbers passed into this function. This gives us the opportunity to create variables/constants like this:

let firstAverage = average(digits: 2,4) //returns 3
let secondAverage = average(digits:3,6,9) //return 6

As you can see, we could pass as many numbers as we’d like, and our function would return the average.

Recommended for you Getting Started With Swift 3.0

In-Out Parameters

In-Out parameters allow you to pass a variable from outside the scope of a function and change it directly inside the scope of it. You are able to take a reference into the function’s scope and send it back out again.

Here’s an example of an inout parameter type function:

fun increaseNum(inout num num:Int, increaser:Int = 2)
    num =+increaser
var total = 0
increaseNum(num:&total) //this returns 2

Based on how we passed our “total” variable into our “increaseNum” function, you can see that we added a “&” sign before it. Because of this, when passing variables into a function with in-out parameters you must include this sign.

If you liked this iOS tutorial SWIFT functions, take a look at some of our other stuff at the tutorial section.

Leave a Reply

Your email address will not be published. Required fields are marked *