Functions in Swift

From the Swift cheat sheet · Functions & Closures · verified Jul 2026

Functions

Function declaration and parameters

swift
// Basic function
func greet(name: String) -> String {
    return "Hello, \(name)!"
}

// Multiple parameters and labels
func greet(person: String, from hometown: String) -> String {
    return "Hello \(person) from \(hometown)"
}
greet(person: "Alice", from: "NYC")

// Default parameters
func increment(number: Int, by amount: Int = 1) -> Int {
    return number + amount
}

// Variadic parameters
func sum(numbers: Int...) -> Int {
    return numbers.reduce(0, +)
}
💡 Use argument labels for clarity at call sites
⚡ Inout parameters allow functions to modify values
📌 Functions are first-class types in Swift
🟢 @discardableResult suppresses unused return value warnings

More Swift tasks

Back to the full Swift cheat sheet