Back to questions
JuniorTheoryCommon
What does the program print?
fun diff(a: Int, b: Int): Int {
return a - b
}
fun main() {
println(diff(10, diff(6, 2)))
}6
- Expecting
2: reading it as10 - 6 - 2from left to right. The inner call runs first and returns4, and only thendiff(10, 4)returns6. - Expecting
-14: thinking the arguments fill the parameters from the end. The first argument goes toaand the second tob, sodiff(6, 2)is4.
- What does
diff(diff(10, 6), 2)print, and why does it differ? - How would you write
diffin one line, with= a - binstead of a block?