When learning Swift, one of the first confusing things you’ll encounter is the difference between functions and methods. They both seem to “do something”, so what’s the real difference? Let’s break it down step-by-step with real-world examples.
What is a Function?
A function in Swift is a block of code that performs a specific task.
You can think of it as a reusable recipe, you give it some ingredients (inputs), and it gives you a result (output). Functions are defined outside of any class, struct, or enum, they stand on their own.
func functionName(parameters) -> ReturnType {
// code to execute
}
Example 1: A Simple Greeting Function
func greet(name: String) -> String {
return "Hello, \(name)!"
}
print(greet(name: "Alice"))
// Output: Hello, Alice!
Here: this func
declares a function. greet
is the function name. and it takes one parameter (name
) and returns a String
. You can call this function from anywhere in your code, it’s not tied to any type or object.
Real-Life Analogy
Think of a function like a microwave:
You can plug it into any kitchen (project), and it works the same way.
It doesn’t “belong” to a specific house (object), it’s general purpose.
What is a Method?
A method is a function that belongs to a specific type, like a class, struct, or enum. So, if a function is independent,
A method is attached to an instance (or type) and can access its properties and data. For example:
struct Car {
var speed: Int
func describe() -> String {
return "The car is going \(speed) km/h."
}
}
Example 2: Instance Method
let myCar = Car(speed: 100)
print(myCar.describe())
// Output: The car is going 100 km/h.
Here, describe()
is a method because it’s defined inside the Car
struct and works with its property speed
.
Example 3: Mutating Method (for Structs)
If a method changes properties of a struct, you must mark it as mutating
.
struct Counter {
var count = 0
mutating func increment() {
count += 1
}
}
var counter = Counter()
counter.increment()
print(counter.count) // Output: 1
Because Counter
is a struct (a value type), we use mutating
to allow modification.
Two Types of Methods
- Instance Methods: Work with an instance of a type (like
myCar.describe()
). - Type Methods: Work on the type itself, not a specific instance (like
Car.numberOfWheels()
).
Example 4: Type Method
class Car {
static func numberOfWheels() -> Int {
return 4
}
}
print(Car.numberOfWheels()) // Output: 4
numberOfWheels()
belongs to the Car
type, not a specific car.
Real-Life Example, Combining Both
Let’s say we’re building a fitness tracker app:
// Function — independent, general-purpose
func convertToMiles(kilometers: Double) -> Double {
return kilometers * 0.621371
}
struct Runner {
var name: String
var distanceInKm: Double
// Method — tied to Runner
func progressReport() -> String {
let miles = convertToMiles(kilometers: distanceInKm)
return "\(name) ran \(miles) miles today!"
}
}
let runner = Runner(name: "Emma", distanceInKm: 5)
print(runner.progressReport())
// Output: Emma ran 3.106855 miles today!
Here: the function convertToMiles()
works globally, can be reused anywhere. And the method progressReport()
belongs to Runner
and calls that function internally. They work beautifully together.
In short, All methods are functions, but not all functions are methods. When you just need reusable logic, use a function. When that logic should be tied to a type’s data or behavior, use a method.
That’s all for this one. If you want to dive deep into functions, check out:
https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions
https://docs.swift.org/swift-book/documentation/the-swift-programming-language/methods
I hope you enjoyed learning functions and methods. I will see you in the next one. Happy learning.