Swift:
Organizing code into functions

How to:

Imagine a task: calculate the average of an array. Without functions, you’d stick it all in main. With functions, you’d do this:

func calculateAverage(of numbers: [Double]) -> Double {
    let sum = numbers.reduce(0, +)
    return numbers.isEmpty ? 0 : sum / Double(numbers.count)
}

// Usage
let scores = [92.5, 88.75, 99.0, 70.5]
let averageScore = calculateAverage(of: scores)
print("Average score is \(averageScore)")

The sample output would be:

Average score is 87.6875

Deep Dive

Historically, as programming grew complex, functions became a keystone for managing complexity. Alternatives include inline coding and copy-pasting code (spaghetti code) – now largely considered bad practice. In Swift, functions are first-class citizens; they can be assigned to variables, passed as arguments, and returned from other functions, making code more modular and flexible.

Implementation-wise, design your functions to do one thing well. Aim for functions with a clear purpose and a name that reflects it. Watch parameter counts—too many and you’re probably doing too much. Error handling? Consider throwing functions and gracefully handle problems. Remember: Swift is all about readability and ease of maintenance.

See Also