Swift: Functions

In this tuturial, we will learn how to create and call functions in Swift.

Why do we use functions?

We use functions for two main reasons: in order to break up the code into more manageable parts but also because we may need to use the same piece of code multiple times and a function makes it quicker to do it.

What is a function?

A function can be seen as a piece of code that will perform a specific task. In swift, we use the “func” word in order to declare it. The name we gave to that function is the keyword we use later on to call a specific function.

A function has 3 different parts:

  • A name – this is how we will call the function in order to use it later when needed
  • Inputs – this is optional, it concerns all the parameters that we declare in the functions that we will use
  • Output – this is optional, not all functions will give an output.

Let’s take a look at the most used types of functions that exist in Swift.

Functions without Parameters and Without Return

This concerns all the functions which do not posses any input parameters and no return. The function starts with the word “func”. The next word is the name of the functions (here: output). Afterwards, between parenthesis, we insert the input parameters, but here it is empty. Finally, we insert the code which will be executed between brackets.

To see the result, we call the function after the function by its name and the parameters inside brackets (here, we let them empty).

Here below is an example which shows a function of this type.

Screen Shot 2017-04-16 at 18.04.00.png

Functions with Parameters

The only difference with respect to the above type of function, is that here we will declare one or several parameters that we will declare between the parenthesis. If we declare several parameters, we will need to separate them with commas. Each parameter has a name that we will use afterwards when calling the function.

Screen Shot 2017-04-16 at 18.28.46.png

We have created a function named “car” that has 3 parameters: the type of the car (the constructor), the model and the year it was built. When we call the function, we will need to write its name and between parenthesis, we will write the parameters names followed by semicolons and then the value we will give to those parameters.

We can also make the function more user friendly and add a name before the parameters. For those parameters that we want to avoid the labelling, we need to put an “_” in front.

Screen Shot 2017-04-16 at 18.32.12.png

So now, when we have called the function, we know exactly what each parameter is about. This is of course optional, in order to facilitate the programming experience.

We can also add a default value to a certain parameter. If this is the case, there is no need anymore to give it a value when we call the function. To do so, we will use the equal sign (“=”) when we declare the variables. Do not forget that you have to omit the declaration of that parameter when you call the function (look at the last line of the code).

Screen Shot 2017-04-16 at 18.34.56.png

Functions with Parameters and Returns

When we deal with returns for a function, we specify the return type by an arrow        “->” after the parenthesis. In the statement of the function, we will need to use the keyword “return”.

Screen Shot 2017-04-16 at 18.48.43.png











One thought on “Swift: Functions

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: