> For the complete documentation index, see [llms.txt](https://ios-course.cornellappdev.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ios-course.cornellappdev.com/resources/textbook/swift-basics/functions.md).

# Functions

Imagine a large scale application with thousands of lines of code. The codebase would be very messy! To solve this, we need to be able to reuse our code. We can do this with **functions**.

Functions allow us to define reusable blocks of code. We define a function by using the `func` keyword followed by the name of the function (`myName`) and open/close parentheses:

```swift
func myName() {
    print("My name is Vin")
}
```

If we were to just define this function in the playground, nothing will be printed out. This is because we also need to **call** the function. We can call the function we previously defined with the following code:

```swift
myName()
```

Let’s test this in the playground:

<figure><img src="/files/LD3fyizhsrh6SliNlFLM" alt=""><figcaption></figcaption></figure>

### Parameters and Arguments

The nice thing about functions is that we can pass in **arguments** to make our functions a lot more useful. Using the example above, let’s customize our function to make it a lot more versatile:

```swift
func myName(name: String) {
    print("My name is \(name)")
}
```

This function has a **parameter** called `name` which is of type `String` and uses string interpolation to output the name. We would then need to pass in an **argument** to the function call:

```swift
myName(name: "Vin")
```

<figure><img src="/files/Bnapo2k8YTSvMiXMlkMN" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
**Parameters and arguments are commonly confused by many. Arguments are passed into the function through the&#x20;*****function call*****. Parameters are variables in the&#x20;*****header*****&#x20;of the&#x20;*****function definition*****.**
{% endhint %}

### External and Internal Parameter Names

In Swift, we can change the way parameters are named in the function call and inside of the function definition.

```swift
func myName(name str: String) {
    print("My name is \(str)")
}

myName(name: "Vin")
```

In this example, the name of the parameter within the function definition is `str` but when we call the function, we use `name`. `str` is known as an **internal parameter** and `name` is called an **external parameter**. This may not seem useful at first glance, but it is a very powerful feature once we begin writing code.

We can also use an underscore (`_`) as the ***external parameter***.

```swift
func myName(_ name: String) {
    print("My name is \(name)")
}

myName("Vin")
```

By doing this, we do not need to provide the external parameter name when passing in our argument in the function call.

{% hint style="info" %}
**Common external parameter names include `in`, `for`, and `with`.**
{% endhint %}

### Returning Values

The functions that we defined earlier did not have any **return value**, meaning that when we called the function, nothing gets sent back to the function caller. However, many of the functions we create will have a return value. To do this in Swift, we use the right arrow (`→`) followed by the return type.

```swift
func isEven(num: Int) -> Bool {
    if num % 2 == 0 {
        return true
    }
    return false
}
```

The function above will return `true` if the argument that we pass in is an even number and `false` otherwise.

<figure><img src="/files/8XgO6Ghv8lq8ztLq6BaS" alt=""><figcaption></figcaption></figure>

Because this function returns a value, we can do many things with this function call such as assigning the returned value to a variable.

<figure><img src="/files/bfH8HbRGf5uBj85csWGq" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
**If our function returns a value with only one line of code, we can omit the `return` keyword. This is commonly seen in SwiftUI.**
{% endhint %}
