Code 101: Functions

Written by: Reinder de Vries, August 1 2015, in Code 101, Guide, Programming

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.

Join 11.000+ app developers and marketers
  • Every Monday: get curated app marketing news, resources, tools and goodies in your inbox
  • Grow your app's userbase with the latest App Store Optimization and app marketing strategies
  • FREE: Get the free step-by-step Get Started With Your App Idea guide to set yourself up for App Store success!
Yes, Sign Me Up!

Popular Posts

Written By: Reinder de Vries

Reinder de Vries is an indie app maker who teaches aspiring app developers and marketers how to build their own apps at He has developed 50+ apps and his code is used by millions of users all over the globe. When he’s not coding, he enjoys strong espresso and traveling.

Get instant access to The Step-by-Step Guide: Get Started With Your App Idea

(PLUS: see the tool we use to save a ton on back-end coding)


Comments & Thoughts

On The Blog

  • My App List: Apps I Use To Be Fitter, Happier, More Productive
    My App List: Apps I Use To Be Fitter, Happier, More Productive

    August 9 2015, in Guide, Programming, Q&A

    It's the list of apps, updated August 2015, of apps I use daily. It's true, these apps make me fitter, happier and more productive. Gems like IFTTT, Trello, and the good ol' notebook, it's all in here.
    Learn more...
  • 10 Networking Tips For Your iOS Development Career
    10 Networking Tips For Your iOS Development Career

    August 28 2016, in Careers, Programming

    So you’re a junior iOS developer, starting out in your new career. But finding your first job isn’t as easy as you thought. You’ve got the skills, but without the experience you’re struggling to even get an interview. Don’t worry, if you follow our tips you’ll soon be getting some much needed human contact again, and a job to boot!
    Learn more...
  • Why You Can't Copy an App from iOS to Android
    Why You Can't Copy an App from iOS to Android

    September 22 2014, in Programming, Q&A

    An often heard question from clients and co-founders is what the difference is between iOS and Android, and why that translates to 2 times the development cost of one individual app. An app is an app, they think. Why can’t you just copy it from iOS to Android?
    Learn more...
  • App Store Publishing Worksheet
    App Store Publishing Worksheet

    October 26 2015, in Guide, Marketing

    Keep this resource handy! It'll help you with publishing your app in the App Store. How? We've outlined the most important App Store fields for you, and gave neat tips for each single one of them. Fill out the fields, share them with your team, and before you know it you've got a shiny App Store page up and running.
    Learn more...