Programming in Swift: Functions & Types

Jan 4 2022 · Swift 5.5, iOS 15, Xcode 13

Part 1: Functions

07. Functions as Parameters

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 06. Challenge: Overloads & Parameters Next episode: 08. Conclusion

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 07. Functions as Parameters

Update Notes: This course was originally recorded in 2019. It has been reviewed and all content and materials updated as of October 2021.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Functions in Swift are simply another data type. You can assign them to variables and constants, and pass them into functions as arguments, just like you can any other type of value, like an Int or a String. Let’s see what that looks like in our Playground.

func add(number1: Int, number2: Int)  {

}
func add(number1: Int, number2: Int) -> Int {

}
  number1 + number2

var function = add
function(4, 2)
func subtract(_ a: Int, _ b: Int) -> Int {
  return a - b 
}
function = subtract
function(4, 2)
func printResult
func printResult(_ operate: 
func printResult(_ operate: 😺(Int, Int) -> Int
func printResult(_ operate: (Int, Int) -> Int, _ a: Int, _ b: Int) {}
  let result = operate(a, b)
  print(result)
printResult(add, 4, 2)
printResult(subtract, 4, 2)
printResult(+, 8, 13)
printResult(-, 8, 13)
printResult(*, 8, 13)
typealias Operate
typealias Operate = (Int, Int) -> Int
func printResult(_ operate: Operate, _ a: Int, _ b: Int) {