Skip to content
Learn Netverks

Lesson

Step 11/36 31% through track

functions-swift

Functions

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Functions: the syntax, patterns, and safety habits you need before advancing in Swift.

Teams still ship Functions in Swift codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Functions in contexts like: iPhone/iPad/Mac apps, server-side Swift (niche), and Apple toolchain projects.

Write Swift in main.swift with print(), click Run on server—the dev runner swiftc compiles and runs the binary (requires Swift toolchain, typically macOS; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Functions are first-class in Swift—named parameters, default values, variadic args, and in-out parameters make APIs readable, similar to Kotlin but with external parameter labels by default.

Syntax and labels

func greet(name: String, excited: Bool = false) -> String {
    let suffix = excited ? "!" : "."
    return "Hello, \(name)\(suffix)"
}

print(greet(name: "Ada"))
print(greet(name: "Ada", excited: true))

External labels appear at call sites; omit with _ when the first argument label would be redundant.

Returning multiple values

Tuples and named return elements avoid temporary wrapper types: func minMax(_ nums: [Int]) -> (min: Int, max: Int).

Important interview questions and answers

  1. Q: Why parameter labels?
    A: Call-site clarity at API boundaries—move(from:to:) reads like English.
  2. Q: Functions as values?
    A: Yes—store in variables, pass to higher-order functions, capture in closures.

Self-check

  1. How do you omit an external parameter label?
  2. Can functions return tuples?

Tip: Omit external labels with _ when the first argument label would read awkwardly at call sites.

Interview prep

Parameter labels?

External labels at call sites for readable APIs—omit with _.

Return tuples?

Yes—named tuple elements avoid wrapper types.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • External vs internal labels?
  • inout params?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump