> 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 %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ios-course.cornellappdev.com/resources/textbook/swift-basics/functions.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
