July 2018
Intermediate to advanced
400 pages
12h 14m
English
Sometimes an argument for a function has a “usual” value. For example, with the castFireball function, five glasses of Fireball is excessive. Typically, only two glasses of Fireball should appear when the spell is cast. To make calling castFireball more efficient, you can use a default argument to specify this.
You saw in Chapter 2 that a var can be assigned an initial value and
later reassigned. Similarly, you can assign a default value for a parameter that will be assigned if no argument is specified.
Update the castFireball function with a default value for numFireballs:
Listing 4.4 Giving the numFireballs parameter a default value (Game.kt)
fun main(args: Array<String>) { ... // Player status printPlayerStatus(auraColor, ...Read now
Unlock full access