Functions and Closures

By the end of this lesson you'll define your own Swift functions with argument labels, default values, variadic and inout parameters — and write closures (Swift's lambdas) to power map and filter. This is how you stop repeating yourself and start building real logic.

Learn Functions and Closures in our free Swift course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Swift course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

1️⃣ Functions, Parameters & Return Types

A function is a named block of code you can run on demand. You declare it with the func keyword, list any parameters (the inputs it needs) in parentheses, and after - state the return type — the kind of value it hands back. If a function has no - Type , it returns nothing; it just performs an action. Read this worked example and run it before moving on.

2️⃣ Argument Labels (External vs Internal Names)

This is Swift's signature feature. Each parameter can have two names: an external argument label that you write when you call the function, and an internal parameter name that you use inside the body. Written func greet(person name: String) , callers say greet(person:) while your code uses name — so calls read like sentences. Put _ as the label to omit it entirely at the call site.

Your turn. Finish the function below so its call reads naturally. Replace the two ___ blanks using the // 👉 hints, then run it and confirm the output.

3️⃣ Default Values & Variadic Parameters

Give a parameter a default value with = value and callers can skip it — you write the common case once instead of overloading the function many times. A variadic parameter , written Type... , accepts zero, one, or many arguments; inside the body it arrives as a plain array, so you can loop or reduce over it.

4️⃣ inout Parameters

By default a function receives a copy of each argument and cannot change the caller's variable. An inout parameter changes that: the function works on the original variable. You must pass the argument with an ampersand — double(&score) — so it is obvious at the call site that the variable may be modified. Only a var can be passed to an inout parameter (never a let or a literal).

5️⃣ Closures (Swift's Lambdas)

A closure is an unnamed function you can store in a variable or hand to another function. Its full shape is {' (params) - ReturnType in body '} . Closures are how Swift does functional programming: map transforms every element of an array, and filter keeps only the elements that pass a test. When the closure is a function's last argument you can use trailing-closure syntax — move it outside the parentheses — and use $0 , $1 as shorthand for its arguments.

Now you try. Fill in the two trailing closures so the array is transformed and filtered as described. The hints show the exact closure to write.

This small program uses everything from the lesson at once — a default value, a variadic parameter, closures with filter and map , and an inout parameter. Read it line by line; you now understand every part.

📋 Quick Reference — Functions & Closures

No blanks this time — just a brief and an outline to keep you on track. Write the function yourself, run it, and check your output against the expected results in the comments. This is exactly the kind of small, reusable logic real apps are built from.

Practice quiz

Which keyword declares a function in Swift?

  • function
  • def
  • fn
  • func

Answer: func. Swift uses func to declare a function.

What does -> Int mean in a function signature?

  • The function returns an Int
  • It takes an Int
  • It loops Int times
  • It is private

Answer: The function returns an Int. -> Type declares the function's return type.

How do you remove the argument label at the call site?

  • Use a colon
  • Write _ as the external label
  • Use a dollar sign
  • Omit the parameter name

Answer: Write _ as the external label. An underscore _ as the external label lets callers omit it.

A variadic parameter is written as...

Type... accepts zero, one, or many values, arriving as an array.

What does an inout parameter let a function do?

  • Return two values
  • Run asynchronously
  • Skip arguments
  • Modify the caller's variable in place

Answer: Modify the caller's variable in place. inout lets the function change the original variable; call it with &.

Which symbol must you put before an inout argument?

  • &
  • *
  • #
  • @

Answer: &. You pass an inout argument with & (e.g. double(&score)).

A closure is best described as...

  • A constant
  • An unnamed function you can store and pass
  • A type of loop
  • A protocol

Answer: An unnamed function you can store and pass. A closure is an unnamed function value you can pass around.

In a trailing closure, $0 refers to...

  • The return type
  • The closure's name
  • The first argument
  • The last element

Answer: The first argument. $0 is shorthand for the first argument, $1 the second.

Which array method keeps only elements passing a test?

  • filter
  • map
  • reduce
  • sorted

Answer: filter. filter returns only the elements for which the closure is true.

What can you pass to an inout parameter?

  • A let constant
  • A literal
  • Any value
  • Only a var

Answer: Only a var. Only a var can be passed to inout, never a let or a literal.