Lesson 004 – Functions

FunctionsSo far, we’ve just been basically executing lines of code in Swift. We’ve written them to be used once, do their work, and then be forgotten about. However, “real programming” isn’t quite so reckless. Programmers seek to create reusable chunks of code that are called methods or functions. There are some differences in the terms if you want to really be pedantic, but most people consider those terms synonyms. In Swift, these reusable chunks of code are referred to as functions.


You should already be familiar with functions, even if you’ve had no previous programming experience. In math class, we learned that the area of a rectangle can be found by multiplying length times width. So, you have a reusable piece of information that works for every rectangle you will encounter. You want an area of a rectangle, you put in a length and a width, it multiplies them and spits out an answer. That’s all a function is.

In the world of iOS and OS X programming, the mainstream way to program in the past has been in Objective-C. In Objective-C, you would declare an instance method like this

Our First Function

This is a fairly confusing syntax and causes some trouble with beginners. I can admit to having some trouble with it myself and even the name of the method can be confusing because the parameters are often named oddly and used as part of the method name itself. Swift aims for a more readable syntax.

In this example, I’ve declared a function using the func keyword. The function takes one parameter (also called an argument by some folks). I’ve named that parameter name so we can use it and I’ve told Swift that we will only be accepting parameters that are of type String. That last little piece that is -> String tells Swift that this function will be returning a String and your program will have to handle that somewhere else.

So, if we had wanted to use this function to print our message to the console, we could do something like this

Returning Void

What if we didn’t want to return anything back, but instead wanted to just do all of the work inside the function? That is possible, too. In that case, we just leave off the -> String part and change the function’s code accordingly. Swift will assume that we are not returning anything. If I rewrite the function to print on its own, I might do something like this

Then, we could just call the method directly to get our output

When I said that Swift would assume that we weren’t returning anything, I was being a little fast and loose with the facts. I apologize. Let me make it right. Truthfully, every function returns something. When you don’t specify a value, Swift assumes you are returning Void. To programmers, that word has the same meaning as it does to normal people. Phrases like “the black void of space” actually capture what we mean. Void here means that we are talking about an empty nothingness. We don’t actually have to make Swift assume anything about our function. Just like we could do -> String to let Swift know we were returning a string, we can use -> Void to let Swift know that we aren’t returning anything at all. Consider this code:

The only difference between this and the first time that I declared the function is that this time, I was explicit in the fact that I wouldn’t be returning anything. Functionally, these two behave the same way. It is up to your aesthetic sensibilities which you prefer. I personally tend to be explicit in this sort of thing to better communicate my intent to anyone else who is reading my code.

Multiple Parameters

If you want to have more than one parameter, you can. You just make a comma separated list of the parameters, using the same syntax over again. Here, I’ve made a function that adds two numbers.

Where this gets a little wonky is in the way you call it. If you are used to other programming languages, you might try to do something like this

This fails with the error message, “error: missing argument label ‘secondAddend:’ in call”.

You actually have to call the function like this

So, the first parameter went in without a label and the second parameter required a label? Maybe you’re thinking that you want to be like me and for consistency’s sake, you want to be explicit in what you’re doing. However, this won’t work either.

If you try that, though, Swift gets angry again. It tells you “error: extraneous argument label ‘firstAddend:’ in call addNumbers(firstAddend: 1, secondAddend: 6)”.

Internal and External Names

So, in its most basic form, you have to do it this way and it can be a little ugly. We only named those parameters so that we could work with them, we didn’t mean to cause anyone any distress. What if there was a way to put very friendly names on those parameters that made them much more readable? Well, there is! Let’s take a look at my improved function.

In this case, our syntax for parameters is to put a “public name” for the parameter, then a “private” or “internal” name for the parameter and then the colon followed by the type. You notice that I still operate on the parameters named firstAddend and secondAddend, but watch how I call this function

Now, I am being consistent in having a label on both parameters. Also, my resulting function call reads like a sentence. “Add the number 8 to the number 9”. For any kind of function that you write that will be used by other people, you should definitely define external parameter names. It will make your resulting code that much better.


We’re going to stop here now that we have the basics of functions down. In the next lesson, we’ll take a look at some more advanced uses of functions in Swift, including: variadic parameters, functions as parameters to other functions, and functions that return other functions are return types.

One comment

Add Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.