Swift 3 Functions that Return Values
These are formatted so you can copy the code straight into your Swift Playground files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
var playerLives:Int = 3 //Below is a function to check if the player still has some lives left. //This part... -> Bool ...means the function will return a Bool value. func canStillPlayGame() -> Bool { if ( playerLives == 0){ //return false if playerLives is zero return false } else { //use an else statement so something is always returned... return true } } // use the function like so... var playerAlive:Bool = canStillPlayGame() //will return true |
If you’re looking for places to learn after these Swift examples, check out CartoonSmart.com or SpriteKitLessons.com