Did you find this helpful? Why not support me to bring you even more content.
Click here to Buy Me a Coffee and say Thanks!

DISCLAIMER: This post is older than one year and maybe out of date.

Functions – The Basics

( NOTE: Updated for Beta 3 syntax changes )

Functions are next up on the basics list, in a class you’ll see them called Methods. Think of functions as blocks of code (not to be confused with the confusing Blocks in previous iOS releases) that perform a task and either return something back or adjust something elsewhere in your program. Functions can be called with parameters that are used in the functions code. Often the function returns some meaningful data back from the call that can then be used for it’s desired purpose.

To put it another way, think of a function as being a dish washer and the dirty plates going in as the parameters. The dish washer does it’s thing and then returns back a clean set of plates ready to be used for something else. In a sudo code language it might be something like this.

The Basics of Creation

In Swift we use the keyword func to define a new function.

So let’s build a new function in Swift using our dish washer sudo code idea as the template.

But it does nothing, accepts nothing as parameters and returns us nothing, so it’s pretty lame yeah! Let’s tell the function to return something to us when it is finished. In our case we will want an array of strings back, so we use the -> to say “Hey give me back something” and then say it is an array of Strings. At this time we also create a variable in the function called cleanItems with is an array to hold our cleaned items and then use the return keyword to tell it to return that array when the function has finished.

That’s better, now it knows to return an array to us when the exciting code has finished and we are ready to exit the function. But now we need to give it a way to take some input to work with so lets tell it to expect an array of strings.

For now lets assume that the new dish washer is not installed yet, plumbers are expensive at the weekends and its a Saturday, so lets just println each of the dirty items as we don’t have hot water or scrubbing bubbles available at this time.

hey fool, the cleanItems are still empty at the end and we won’t care about printing the items to the screen when the dishWasher is installed on Monday!

Yeah I know, let’s take care of that and add each item after it has been cleaned to the array that gets returned at the end of the function and remove the println.

Here we are simply using the push command to add the just cleaned dirtyItem to the cleanItems array each time we loop through the array.

Play time

Now let’s try using the function by creating a new array of items of type String and sending it as an argument, time to crack open your playground and explore!

Default values in parameters

There are times you might not have a value to send for a particular parameter or you may find you send the same value over and over. For that you could use a default value, in this example we give the parameter name a default string value and then call it without providing a parameter value. If you do send a parameter value then it will replace the default. By the way using the name of the parameter as you call the function can really help out in more complicated scenarios, so you really should get used to thinking that way.

Multiple Parameters

Sending more than one argument to a function is super easy just separate them with a comma. Notice on the third call we switched the parameters around the other way, but since we named them everything works as expected, see I told you it was worth it! On the fourth call we do not send a name so the default is used however we use the parameter value for the greeting.

Returning Multiple Values a.k.a. Hello Mr Tuple

Swift has something cool, it allows us to return multiple values using something called a Tuple. We do not have to do any clever tricks like we do with other languages to get multiple values returned….rejoice! Now tuples can get as complicated as you want them to be, but here we will go over the basics to get you up and running. To return a tuple you simply use a comma separated list in the return type.

But how do you get to the values you ask, well you decompose them, try this out using the above function

But that’s not all! If you name the values in the return then you can use cleaner code to access them.

Now that looks cleaner and easier to understand does it not? Hey in life it’s the small things, and in code those small things can prevent big disasters when the code gets complicated because those project requirements change…never happens right? 🙂