Code 101: Functions

Written by: Reinder de Vries, August 1 2015, in App Development

Code 101: Functions

OK, let’s talk about functions for a second. Fundamentally, programming code is made up of 2 building blocks: variables and functions. Just like a natural spoken language is made up of nouns and verbs, things and actions, programming is made up of variables and functions.

In this episode of Code 101 we’ll learn a thing or two about functions. Let’s go!

What Is A Function?

Functions are self-contained chunks of code that perform a specific task. It’s an abstraction, defined in one place and called upon in another. In essence, a computer program is nothing more than a structured bunch of defined functions, and calls to these functions.

In Swift programming, you often talk about methods instead of functions. They’re essentially the same: the only difference is that methods are part of a class, and functions aren’t members of anything. More on classes and methods in a future Code 101.

Function Definition
Function Definition

How Do You Define A Function?

In the above image you see a function definition. It’s a distinct part of programming with functions, and has the following characteristics:

  1. Any Swift function starts with the keyword func. It’s followed by a space.
  2. Each and every function has a name, in the example above it’s greet.
  3. A function can have zero or more parameters. They’re the input for the function. Parameters are separated with a comma, and start with the parameter name, followed by a semi-colon, followed by the parameter type. The parameters are encapsulated in parenthesis (round brackets). In the example above, the function has one parameter with name name and type String.
  4. The contents of the function is ordinary Swift code. It’s encapsulated in squiggly brackets, and in our example it outputs a string of text to the Xcode console.

How Do You Use A Function?

Another distinct part of programming with functions, is the function call. The definition of a function doesn’t do anything yet, a function can only be used when it is called upon.

Function Call
Function Call

In the above image you see a function call (also called invocation). When this line of code is executed, the contents of the function is executed: it prints out the println line to the console.

In the example image above, a function call has 2 parts:

  1. The name of the function. We defined it earlier: it’s greet.
  2. The arguments of the function. It’s written between parenthesis, separated by commas. Each function parameter corresponds to a function argument. You could say: a parameter is the parking spot, and the argument is the automobile.

Why Do We Need Functions?

You could see the call to a function as a “connection”. A particular code chunk is abstracted into a function with a name, and called upon. In the examples above, instead of writing the actual println line I write the name of the function.

The cool thing about functions is: I can now use the code block anywhere in my code! What if I need to say Hello! to a hundred people? I can write the function once, and call it a hundred times. Less code means less errors, and that’s good for all of us.

Intermediate: Named Parameters And Function Returns

Some functions return stuff. They execute a particular action, and then return the result of that action.

Like this:

func greet(name: String, greeting:String) -> String  
    var result = "\(greeting), \(name)"

    return result;  

In this code snippet, we’ve added 4 things to the greet function:

  1. A second parameter is added called greeting of type String.
  2. A return type is added: -> String. This function returns a value of type String.
  3. A variable result is added, store the result of the expression "\(greeting), \(name)".
  4. The return keyword is added. The function returns the variable result. This variable is of type String, just like the return type declared.

And this is the entire code sample:

Named Parameters And Function Returns
Named Parameters And Function Returns

As you can see, the two arguments "Ford" and "Good Morning" end up in the function, and are used to create the result string. The result is return, and is stored in variable text outside the function. The text variable is then printed to the console with the println function.

Another cool thing happens: we use a named parameter. In Swift, parameters from the second one on must be named. Look at these 2 code lines:

var text = greet("Ford", "Good Morning")  
var text = greet("Ford", greeting: "Good Morning")

On the first line, we make a call to function greet and provide it 2 arguments. On the second line we do the same, but before the second argument we write greeting. This is a named parameter, in addition to writing the argument we also write the name of the parameter.


And… that’s all there is to it! Functions make up the distinct code blocks in Swift code. Now you know how you can write them, and how you can invoke them.

Reinder de Vries

Reinder de Vries is a professional iOS developer. He teaches app developers how to build their own apps at Since 2009 he has developed a few dozen apps for iOS, worked for global brands and lead development at several startups. When he’s not coding, he enjoys strong espresso and traveling.

Leave a Reply

Required to post: Your real name and email address, and a pleasant demeanor. Your email address will not be published. Markdown is supported.