Swift Tutorial
Swift Data Types
Swift Control Flow
Swift Functions
Swift Collections
Swift OOPs
Swift Additional Topics
In Swift, a function is a reusable piece of code that performs a specific task. You can define and call functions to execute these tasks.
Here's a basic introduction to defining and calling functions in Swift:
Here's a simple function that prints a greeting:
func greet() { print("Hello, World!") }
Once a function is defined, you can call it using its name followed by parentheses:
greet() // Outputs: Hello, World!
Functions can take parameters to make them more versatile:
func greet(person: String) { print("Hello, \(person)!") }
greet(person: "Alice") // Outputs: Hello, Alice!
func greet(person: String, day: String) { print("Hello, \(person)! Today is \(day).") }
greet(person: "Bob", day: "Tuesday") // Outputs: Hello, Bob! Today is Tuesday.
Functions can return values using the ->
syntax followed by the return type:
func add(a: Int, b: Int) -> Int { return a + b }
let sum = add(a: 5, b: 3) print(sum) // Outputs: 8
Swift allows you to omit parameter labels using an underscore (_
):
func multiply(_ a: Int, _ b: Int) -> Int { return a * b } let result = multiply(4, 5) print(result) // Outputs: 20
You can provide default values for parameters:
func greetWithDefault(person: String, day: String = "Sunday") { print("Hello, \(person)! Today is \(day).") } greetWithDefault(person: "Charlie") // Outputs: Hello, Charlie! Today is Sunday.
Swift allows you to create functions that accept a variable number of arguments using the ...
syntax:
func sum(numbers: Int...) -> Int { var total = 0 for number in numbers { total += number } return total } let total = sum(numbers: 1, 2, 3, 4, 5) print(total) // Outputs: 15
Remember, functions are a fundamental aspect of any programming language, and Swift is no exception. Getting familiar with the syntax and conventions of Swift functions will help you write more efficient and readable code.
How to call functions in Swift:
func greet(name: String) { print("Hello, \(name)!") } greet(name: "John")
Swift function parameters and arguments:
func addNumbers(a: Int, b: Int) -> Int { return a + b } let result = addNumbers(a: 5, b: 3)
Swift function return types:
->
.func multiply(a: Int, b: Int) -> Int { return a * b } let product = multiply(a: 4, b: 2)
Calling methods in Swift:
struct Math { func square(number: Int) -> Int { return number * number } } let math = Math() let squaredValue = math.square(number: 3)
Swift function overloading examples:
func process(value: Int) { // Implementation } func process(value: String) { // Implementation }
Swift function with default parameters:
func greet(name: String = "Guest") { print("Hello, \(name)!") } greet() // Prints "Hello, Guest!" greet(name: "John") // Prints "Hello, John!"
Swift function with inout parameters:
inout
parameters allow modifying the original value.func increment(number: inout Int) { number += 1 } var count = 5 increment(number: &count)
Swift function closures and calling:
let multiplyClosure: (Int, Int) -> Int = { a, b in return a * b } let result = multiplyClosure(3, 4)
Swift function naming conventions:
func calculateTotalAmount(itemPrice: Double, quantity: Int) -> Double { // Implementation }
Swift function error handling:
throws
for functions that can throw errors and do-catch
for handling errors.enum MathError: Error { case divisionByZero } func divide(a: Double, b: Double) throws -> Double { guard b != 0 else { throw MathError.divisionByZero } return a / b } do { let result = try divide(a: 10, b: 0) } catch { print("Error: \(error)") }
Advanced function calling techniques in Swift:
let numbers = [1, 2, 3, 4, 5] let sum = numbers.reduce(0, +)