Swift Function Parameters
Passing data into functions for dynamic behavior
📥 What are Function Parameters?
Parameters allow functions to receive input data, making them flexible and reusable. They act like variables that hold values passed to the function when called.
// Function with a parameter
func greetUser(name: String) {
print("Hello, \(name)!")
}
// Call with different values
greetUser(name: "Alice")
greetUser(name: "Bob")
Output:
Hello, Alice!
Hello, Bob!
Parameter Concepts
Parameter Label
Name used when calling the function
func greet(name: String) {
print("Hi \(name)")
}
Parameter Type
Specify what kind of data to expect
func add(a: Int, b: Int) {
print(a + b)
}
Multiple Parameters
Functions can accept several inputs
func info(name: String, age: Int) {
print("\(name) is \(age)")
}
Default Values
Provide default parameter values
func greet(name: String = "User") {
print("Hello \(name)")
}
🔹 Basic Parameter Syntax
Parameters are defined inside parentheses with name and type:
func functionName(parameterName: ParameterType) {
// Use parameterName inside function
}
// Example: Function with one parameter
func displayMessage(text: String) {
print("Message: \(text)")
}
// Call the function with different values
displayMessage(text: "Welcome!")
displayMessage(text: "Good morning!")
displayMessage(text: "See you later!")
Output:
Message: Welcome!
Message: Good morning!
Message: See you later!
🔹 Multiple Parameters
Functions can accept multiple parameters separated by commas:
// Function with multiple parameters
func calculateArea(width: Double, height: Double) {
let area = width * height
print("Area: \(area) square units")
}
func createProfile(name: String, age: Int, city: String) {
print("Name: \(name)")
print("Age: \(age)")
print("City: \(city)")
print("---")
}
// Call functions with multiple arguments
calculateArea(width: 5.0, height: 3.0)
createProfile(name: "Sarah", age: 25, city: "New York")
Output:
Area: 15.0 square units
Name: Sarah
Age: 25
City: New York
---
🔹 Default Parameter Values
You can provide default values for parameters:
// Function with default parameters
func orderCoffee(size: String = "Medium", sugar: Bool = false) {
print("Ordering \(size) coffee")
if sugar {
print("With sugar")
} else {
print("No sugar")
}
print("---")
}
// Call with different combinations
orderCoffee() // Uses all defaults
orderCoffee(size: "Large") // Custom size, default sugar
orderCoffee(sugar: true) // Default size, custom sugar
orderCoffee(size: "Small", sugar: true) // Custom both
Output:
Ordering Medium coffee
No sugar
---
Ordering Large coffee
No sugar
---
🔹 Parameter Labels vs Names
Swift allows different external and internal parameter names:
// External label 'for' and internal name 'person'
func sendMessage(for person: String, with content: String) {
print("Sending to \(person): \(content)")
}
// Omit external labels with underscore
func multiply(_ a: Int, _ b: Int) {
let result = a * b
print("\(a) × \(b) = \(result)")
}
// Call functions
sendMessage(for: "John", with: "Hello!")
multiply(5, 3)
Output:
Sending to John: Hello!
5 × 3 = 15