Swift Functions

A portrait painting style image of a pirate holding an iPhone.

by The Captain

on
June 5, 2023
Swift Functions

Swift Functions

In Swift, a function is a self-contained block of code that performs a specific task. Functions help in defining reusable code blocks and make the code more modular, readable, and maintainable. Functions can have one or more parameters and can also return values.

Defining Functions

A Swift function is defined using the func keyword followed by the function name, parameters, and the return type (if any).


	func greet(name: String) {
	    print("Hello, \(name)!")
	}
	
	greet(name: "John")
	    
	func addNumbers(a: Int, b: Int) -> Int {
	    return a + b
	}
	
	let result = addNumbers(a: 2, b: 3)
	

Function Parameters

Functions in Swift can have parameters that can be either of constant or variable type. Also, there are two types of parameters:

  • Named Parameters: Named parameters are used to explicitly identify the meaning of each parameter in function call.
  • Default Parameters: Default parameters are used to define default values for the function parameters if no value is supplied explicitly.

	func multiplyNumbers(a: Int, b: Int = 2) -> Int {
	    return a * b
	}
	
	let result1 = multiplyNumbers(a: 3) // Returns 6
	let result2 = multiplyNumbers(a: 3, b: 4) // Returns 12
	

Function Return Types

A function in Swift can return a value or void (if it doesn't return any value). The return type of a function is specified using the arrow -> followed by the return type.


	func isEven(number: Int) -> Bool {
	    if number % 2 == 0 {
	        return true
	    }
	    else {
	        return false
	    }
	}
	
	let result = isEven(number: 4) // Returns true
	

Function Types

In Swift, functions are the first-class citizens, this means that functions can be assigned to variables/constants, passed as parameters to other functions, and can also be returned from functions.


	func subtractNumbers(a: Int, b: Int) -> Int {
	    return a - b
	}
	
	var mathFunction: (Int, Int) -> Int = addNumbers
	print(mathFunction(2, 3)) // Prints 5
	
	mathFunction = subtractNumbers
	print(mathFunction(3, 2)) // Prints 1
	

Function Overloading

Swift allows defining multiple functions with the same name but with different parameters (type or number).


	func sayHello() {
	    print("Hello, world!")
	}
	
	func sayHello(name: String) {
	    print("Hello, \(name)!")
	}
	
	sayHello() // Prints "Hello, world!"
	sayHello(name: "Joe") // Prints "Hello, Joe!"
	

The @discardableResult Attribute

Functions in Swift returns a value by default, but sometimes, we may not need the return value in our code. In such cases, we can use the @discardableResult attribute to ignore the return value.


	@discardableResult
	func printMessage(message: String) -> Bool {
	    print(message)
	    return true
	}
	
	printMessage(message: "This is a message") // Prints "This is a message"
	

Summary

Functions in Swift are a powerful tool to define reusable code blocks that perform specific tasks. They can have one or more parameters, a return value, and can also use named or default parameters. Functions can be assigned to variables/constants, passed as parameters, and returned from other functions. Swift supports function overloading and the @discardableResult attribute to ignore a function's return value.