Swift programming on laptop.

Blog Tech Tutorials Swift Functions

Swift Functions

In this article, we will go over what a Swift function is, how to write a Swift function, how to insert values into Swift functions, and how Swift functions can return values.  Let’s get started!

What is a function?

A function is a piece of code that does something.  What your function does, is up to you.

For example:

  • Submit a form
  • Open an account page
  • Calculate the fastest route between two points

At the most basic level, this is what a function does.  Functions are very useful because they are compartmentalized chunks of code that can be called upon to do a task over and over.

How do I write a Swift function? (Declaring a function)

In Swift, you use the keyword func to declare a function.  It is followed by the function name with open and closed parenthesis () with the function statement within curly braces {}.

Swift function structure:

func functionName(){

Let’s create your first Swift function.  Start with the word func followed by your function name with open and closed parenthesis:

func myFirstFunction()

Next, let’s add your function statement within curly braces.  Let’s use the print statement to display on the console:

//writing my first function
func myFirstFunction(){
    print("This is my first function! Yay!")

In this example, “myFirstFunction” is the function name, while the function statement prints out a string of text to the console.  If you were able to follow along this far, congrats!  You just declared your first Swift function.

Calling a Swift function

Now that we have declared our Swift function, we need to call it in order to use it.  In Swift, you call a function by writing the function name followed by parenthesis.

Let’s try to call our “myFirstFunction” function:

//Calling my function


This is my first function! Yay!

If you run this on Swift Playgrounds, you’ll see the same result:

If you wish to try this on your own without installing Swift Playgrounds, here’s a way to do it online. https://replit.com/languages/swift

Putting values in Swift functions (parameters)

Let’s go a little deeper. Take a look at the parenthesis – you can actually insert a parameter with a type so that your output changes based on the value you use.  (Note: You must always declare what type you will return since the Swift language is a strongly typed language. )

Going back to our function structure:

//Swift function structure
func functionName(parameter: type){

Let’s create a function that greets with your friend’s name based on their name:

func greetFriend(friendName: String){
    print("Ahoy there, " + friendName)

Let’s call our function:

greetFriend(friendName: "Michael")


Ahoy there, Michael

What if, instead of Michael, we want to greet Melissa? Parameters can make that happen!

greetFriend(friendName: "Melissa")


Ahoy there, Melissa

Swift functions can return values

Let’s go another layer deep. What if, instead of just entering values, I wanted to get a return value?

Let’s go back to our function structure:

//Swift function structure
func functionName(parameter: type) -> type {
    return returnStatement 

When adding a return, we must declare what return type it is within our function declaration. Additionally, we need to include the word return at the end of our function statement and say what we want to return.

Let’s create a function that will tell us how much money we will get if we work x number of hours at x rate per hour:

func calculatePay(hourlyRate: Int, hoursWorked: Int) -> Int {
    var totalPay = hourlyRate * hoursWorked
    return totalPay

Let’s call our function:

calculatePay(hourlyRate: 40; hoursWorked: 20)


Hmm, why am I not seeing anything in the console?  Return does not necessarily mean print.  In order to print onto the console, we need to encapsulate the return inside a print function:

print("Total pay: ",calculatePay(hourlyRate:40, hoursWorked:20)


Total pay: 800

And, there you have it!  An introduction to Swift functions. In a future blog post, we will delve into a further discussion of functions with Swift closures. Happy coding!

Want to learn more about Swift? Explore Udacity’s Become an iOS Developer Nanodegree.

Stay updated on the latest in technology

"*" indicates required fields

Stay updated on the Latest in Tech

Be first to hear about special offers, news, and resources from Udacity

"*" indicates required fields