Functions and Methods
Named Arguments
Call a method by naming the parameters.
named-arguments
Named arguments make a call read like a small label-value list.
Named Arguments
NamedArguments.scala
Replay: real traced execution (multi-file project)
object Main {
def totalCost(price: Int, quantity: Int): Int = {
price * quantity
}
def main(args: Array[String]): Unit = {
val price = 12
val quantity = 2
val total = totalCost(quantity = quantity, price = price)
println("price=" + price)
println("quantity=" + quantity)
println("total=" + total)
}
}
object Main {
def totalCost(price: Int, quantity: Int): Int = {
price * quantity
}
def main(args: Array[String]): Unit = {
val price = 8
val quantity = 2
val total = totalCost(quantity = quantity, price = price)
println("price=" + price)
println("quantity=" + quantity)
println("total=" + total)
}
}
object Main {
def totalCost(price: Int, quantity: Int): Int = {
price * quantity
}
def main(args: Array[String]): Unit = {
val price = 20
val quantity = 2
val total = totalCost(quantity = quantity, price = price)
println("price=" + price)
println("quantity=" + quantity)
println("total=" + total)
}
}
price ← 12, quantity ← 2
6def main(args: Array[String]): Unit = {7 val price→ 12 = 12 //@price=8, 208 val quantity→ 2 = 29 val total = totalCost(quantity = quantity2, price = price12)1011 println("price=" + price)def totalCost(price: Int, quantity: Int): Int =
1object Main {2 def totalCost(price12: Int, quantity2: Int): Int = {3 price12 * quantity24 }total ← 24, quantity ← 2, price ← 12
8 val quantity = 29 val total→ 24 = totalCost(quantity→ 2 = quantity, price→ 12 = price)1011 println("price=" + price12)12 println("quantity=" + quantity2)13 println("total=" + total24)14 }15}outputprice=12 quantity=2 total=24
price ← 8, quantity ← 2
6def main(args: Array[String]): Unit = {7 val price→ 8 = 88 val quantity→ 2 = 29 val total = totalCost(quantity = quantity2, price = price8)1011 println("price=" + price)def totalCost(price: Int, quantity: Int): Int =
1object Main {2 def totalCost(price8: Int, quantity2: Int): Int = {3 price8 * quantity24 }total ← 16, quantity ← 2, price ← 8
8 val quantity = 29 val total→ 16 = totalCost(quantity→ 2 = quantity, price→ 8 = price)1011 println("price=" + price8)12 println("quantity=" + quantity2)13 println("total=" + total16)14 }15}outputprice=8 quantity=2 total=16
price ← 20, quantity ← 2
6def main(args: Array[String]): Unit = {7 val price→ 20 = 208 val quantity→ 2 = 29 val total = totalCost(quantity = quantity2, price = price20)1011 println("price=" + price)def totalCost(price: Int, quantity: Int): Int =
1object Main {2 def totalCost(price20: Int, quantity2: Int): Int = {3 price20 * quantity24 }total ← 40, quantity ← 2, price ← 20
8 val quantity = 29 val total→ 40 = totalCost(quantity→ 2 = quantity, price→ 20 = price)1011 println("price=" + price20)12 println("quantity=" + quantity2)13 println("total=" + total40)14 }15}outputprice=20 quantity=2 total=40