Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. SwiftBeginner - What are Functions?

What are Functions? - Images: Japanese

Hey there, everyone!

Next up, let's talk about 'functions'. We'll start from the basics — what exactly is a function?

So far we've covered variables and constants.

Variables and constants were basically "a way to give data a name so you can refer to it and work with it whenever you need." Arrays and dictionaries follow a similar concept.

let love = "I love you!!"

print(love) // You can call it anytime.

print(love) // Anytime, as many times as you want.

print(love) // You can express your feelings anytime, as many times as you want. If you don't say it, it won't get through.

With functions, the idea is similar — but instead of naming data, you're "giving a name to a process (operation) so you can call it and run it whenever you need."

That might sound a bit abstract, so let's use an example. If you want to store the value 1, you'd use a variable or constant, right?

let n = 1 // Store the value '1' in the constant 'n'.

Now say you have an expression like '1 + 1', and you want to be able to call that operation itself whenever you like — that's exactly where functions come in.

So let's define a function. It looks like this.

func test() {
    print(1 + 1)
}

In the example above, we've given the process print(1 + 1) the name test, so it can be called at any time.

Let's go over the syntax. You'll notice func at the start of the line.

This is like a declaration telling the computer "I'm defining a function!" — and it's required whenever you define a function. Think of it like var or let when defining variables or constants.

After func, add a space and then write the function name. In the example above, that's test.

One thing to note: Swift recommends 'lower camel case' as the naming convention for function names, so unless you have a specific reason not to, stick with lower camel case.

Lower camel case means "names where the first word starts with a lowercase letter, and each subsequent word starts with an uppercase letter."

For example, "this is a pen" in lower camel case becomes 'thisIsAPen'.

There's also 'upper camel case', where the first word also starts with a capital letter. So "this is a pen" in upper camel case becomes 'ThisIsAPen'.

There are also 'snake case' and 'chain case'. Snake case uses '_' as the word separator — "this_is_a_pen" — while chain case uses '-' — "this-is-a-pen".

There's quite a bit to remember here, but these terms come up a lot in coding conventions across programming languages, so it's worth keeping them in mind.

The valid identifiers for function names in Swift are largely the same as for variable names.

That said, Swift's naming rules for identifiers tend to change between versions, so it's probably safer to avoid using unusual characters or symbols as identifiers.

We covered naming rules here, so check that out if you need a refresher.

After the function name, you place () and {}. You can add spaces around them or not — it's up to personal preference, so go with whatever you like.

// Both of these are fine.
func test () {}

func test1(){}

Then inside {} you write the process (operation). Here we have print(1 + 1). If you want to run both print(1 + 1) and print("Hatsune Miku") at the same time, you'd do it like this.

func test() {
    print(1 + 1)
    print("Hatsune Miku")
}

Nothing tricky here — you just write them out one after another.

One thing to keep in mind though: it's considered standard practice to indent the contents inside {}. This is a common convention in most programming languages, so make a habit of it.

func test() { // This style is generally frowned upon.
print(1 + 1)
print("Hatsune Miku")
}

For more on indentation, see this article.

And that covers defining a function.

Now let's look at how to call the function we defined and actually run it. It goes like this.

func test() {
    print(1 + 1)
}

test() // Outputs the value '2'.

Take a look at test() on that last line. Writing functionName() like this is how you call and run a defined function. Don't forget the () — that's what actually triggers the function to execute.

So that's defining and calling functions.

For now though, the process is fixed as print(1 + 1), which isn't very versatile. In the next article, we'll cover arguments to make functions more flexible. See you there!

This article was written by Sakurama.

Author's beloved small mammal

桜舞 春人 Sakurama Haruto

A Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him.

If you find any errors or copyright issues, please .