Skip to main content
Swift for iOS Development
CHAPTER 06 Beginner

Functions and Closures in Swift

Updated: May 16, 2026
6 min read

# CHAPTER 6

Functions and Closures in Swift

1. Introduction

Imagine you are building a calculator app. If you have to type out the math logic for addition every single time the user presses a button, your code will become thousands of lines long and impossible to read. Good programmers are lazy; they write code once and reuse it a million times. In this chapter, we will master Functions and Closures. We will learn how to encapsulate logic into reusable machines, pass data into them using parameters, extract data using return types, and utilize Swift's incredibly powerful (and sometimes confusing) feature known as Closures.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define and call basic functions in Swift.
  • Create functions that accept external data (Parameters).
  • Create functions that output data (Return Values).
  • Understand the syntax of Closures (Anonymous Functions).
  • Utilize Trailing Closure syntax in modern Swift APIs.

3. What is a Function?

A function is a self-contained block of code that performs a specific task. You define it once, and then "call" it whenever you need it. In Swift, functions are declared using the func keyword.
swift
12345678
// 1. Defining the function
func greetUser() {
    print("Welcome back to the application!")
    print("Fetching your data...")
}

// 2. Calling the function (You can do this 100 times!)
greetUser() 

4. Parameters (Inputting Data)

A function that does the exact same thing every time is boring. We want functions to act like machines: put in raw materials, get a result. We pass data into a function using Parameters.
swift
12345678
// This function expects a String to be passed into it!
func sayHello(to name: String) {
    print("Hello, \(name)! Have a great day.")
}

// We must provide the 'to' argument label!
sayHello(to: "Alice")
sayHello(to: "Bob")

Multiple Parameters:

swift
123456
func calculateDamage(baseDamage: Int, multiplier: Int) {
    let total = baseDamage * multiplier
    print("You dealt \(total) damage!")
}

calculateDamage(baseDamage: 20, multiplier: 3)

5. Return Values (Outputting Data)

Instead of just printing a result, functions often need to give data *back* to the rest of the program so it can be saved in a variable. We use the -> arrow to tell Swift exactly what type of data the function will return.
swift
123456789
// The arrow '-> Int' means this function promises to output an Integer!
func multiply(a: Int, b: Int) -> Int {
    let result = a * b
    return result // The 'return' keyword spits the data back out!
}

// Now we can save the output into a variable!
let finalScore = multiply(a: 5, b: 10)
print(finalScore) // Prints 50

6. What are Closures?

A Closure is exactly like a function, but it doesn't have a name. It is an "Anonymous Function". Why would you want a function without a name? Because in Swift, you can pass functions around like variables! Closures are heavily used in iOS for animations, network requests, and sorting lists.
swift
12345678910
// Normal Function
func addNumbers(x: Int, y: Int) -> Int { return x + y }

// Closure (Assigned directly to a variable!)
let addClosure = { (x: Int, y: Int) -> Int in
    return x + y
}

// Calling the closure
let sum = addClosure(5, 5)

7. Trailing Closures (SwiftUI Magic)

If a function accepts a closure as its *very last parameter*, Swift allows a special, ultra-clean syntax called a Trailing Closure. You will see this EVERYWHERE in SwiftUI.
swift
123456789101112131415
// A function that downloads data, then executes a closure when it finishes.
func downloadData(completion: () -> Void) {
    print("Downloading...")
    completion() // Execute the closure!
}

// The old, ugly way to call it:
downloadData(completion: {
    print("Finished!")
})

// The Modern Trailing Closure way! (Look how clean it is!)
downloadData {
    print("Finished!")
}

8. Common Mistakes

  • Forgetting Argument Labels: When calling a function in Swift, you MUST type out the parameter names unless the developer explicitly hid them. calculateDamage(20, 3) will crash the compiler. You must write calculateDamage(baseDamage: 20, multiplier: 3).
  • Missing Return Keyword: If your function definition has an arrow -> String, the compiler will refuse to build the app until you actually write return "Some String" inside the function block.

9. Best Practices

  • Single Responsibility Principle: A function should do exactly ONE thing. If your function is named calculateScore, it should only do math. It should NOT also save the score to the database and change the color of the screen. If a function is longer than 20 lines, break it into smaller functions!

10. Exercises

  1. 1. Write a function named sayGoodbye that accepts a name: String parameter and prints "Goodbye, [name]!".
  1. 2. Write a function named isEven that accepts a number, and returns a Bool (true if even, false if odd).

11. Coding Challenges

Challenge: Write a function named calculateTotal that accepts a price: Double and a taxRate: Double. The function must calculate the total price and use the -> operator to return the final Double. Test it by saving the output to a constant.

12. MCQ Quiz with Answers

Question 1

In Swift, what symbol is used in a function definition to explicitly state the data type that the function will output back to the caller?

Question 2

What is the defining characteristic of a Swift Closure?

13. Interview Questions

  • Q: Explain the purpose of Argument Labels in Swift functions. How can a developer omit an argument label when calling a function using the _ syntax?
  • Q: Contrast a standard Function with a Closure. Provide a specific iOS development scenario where a Closure is fundamentally required (e.g., asynchronous networking).
  • Q: Describe "Trailing Closure" syntax. Why did Apple heavily optimize this syntax design specifically for the introduction of SwiftUI?

14. FAQs

Q: Can a function return two things at once? A: Yes! Swift allows returning Tuples. You can define a function as func getCoordinates() -> (x: Int, y: Int). It will return both values bundled together!

15. Summary

In Chapter 6, we transformed our code from a linear script into modular, reusable architecture. We utilized the func keyword to encapsulate logic, injected raw data via explicitly labeled Parameters, and extracted computed data using the -> Return syntax. Finally, we encountered the advanced concept of Closures, uncovering how passing anonymous functions as variables enables the powerful Trailing Closure syntax that drives modern SwiftUI development.

16. Next Chapter Recommendation

Functions are great for actions, but what about "Things"? How do we build a User that has a name, an age, AND a function to calculate their score? Proceed to Chapter 7: Object-Oriented Programming in Swift.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·