Lynda Course - Swift 5 Essential Thinking (Part 4)
Am smashing through this course and enjoying it! Probably because it's mostly recap and consolidation. Anyway, got a bit of time before a brunch today so no time to waste!
Start Time - 11:26
5. The Wide World Of Functions
So a point here is that a lot of these are already built into Swift. But we can create our own of course!
Basic functions
All good recap here.
Overloading functions
Heard of these once before in Pasan's story course. So this is using the same name for multiple functions, as long as their signatures are unique.
Start Time - 11:26
5. The Wide World Of Functions
So a point here is that a lot of these are already built into Swift. But we can create our own of course!
Basic functions
All good recap here.
Overloading functions
Heard of these once before in Pasan's story course. So this is using the same name for multiple functions, as long as their signatures are unique.
// Base function
func attackEnemy() {
print("Attacking enemy...")
}
// Overloaded functions
func attackEnemy(damage: Int) {
print("Attacking enemy; \(damage) caused")
}
func attackEnemy(damage: Double, weapon: String) -> Bool {
if damage > 8.4 {
return true
} else {
return false
}
}
attackEnemy(damage: 7.5, weapon: "Axe")
Complex Functions
/ Optional return value
func setupArenaMatch() -> Bool? {
return false
}
if let initSuccess = setupArenaMatch() {
print("Level initialised: \(initSuccess)")
} else {
print("Level not initialised")
}
Couldn't keep up with the speed so am just screenshotting -
SO this is cool!
// Default values
func setupDefaultMatch(levelName: String = "Fire marshes", opponents: Int = 3) {
print("Arena match will take place in the \(levelName) between \(opponents) opponents")
}
setupDefaultMatch()
Something I had forgotten - you can create a function with default values...BUT you can still override them upon declaration!
Function Types
func computeBonusDamage(baseDamage: Int) -> Int {
return baseDamage * 4
}
Here we have a function signature of (Int) -> Int. That means I can use that signature as a parameter - as long as it matches!
// Function types
func computeBonusDamage(baseDamage: Int) -> Int {
return baseDamage * 4
}
// Functions as parameters
func dealDamage(baseDamage: Int, bonusDamage: (Int) -> Int) {
let bonus = bonusDamage(baseDamage)
print("Base damage: \(baseDamage)\n Bonus damage: \(bonus)")
}
dealDamage(baseDamage: 4, bonusDamage: computeBonusDamage)
Good stuff! This will bring us on to closures...
Understanding Closures
A couple of things to unpick -
An empty closure..
var closure: () -> () = { }
And now how to implement one....
// Initializing closures
var computeBonusDamage: (Int) -> Int = { (base: Int) -> Int in
return base * 4
}
var computeBonusDamage: (Int) -> Int = { base in
return base * 4
}
And doing this myself -
var computeBonusDamage: (Int) -> Int = { $0 * 4 }
Using closures
I am a bit lost with all of this - suddenly got a lot harder! Will see if I can make sense of it in the challenge...
Type Alias
Yes I love these!
Stopped at 12:18 (52 minutes)
I'm racing along to try to get these done but actually need to go over this. So I will not be completing Chapter 5 today, that's fine! Next time I will go over the using closures bit, go back through type alias as the code is getting complex. Then I will attempt the challenge! Good stuff.
Comments
Post a Comment