DAY 5
0
Mobile Development

## Function Types

``````fun plus(num1: Int, num2: Int): Int{
return num1+ num2
}
``````

``````fun plus(num1: Int, num2: Int): Int = num1+ num2
``````

``````val plus = fun (num1: Int, num2: Int): Int = num1+ num2
``````

``````textView.text= plus(5, 7).toString()
``````

## 高階函數

``````fun calculate(
num1: Int,
num2: Int,
operator: (Int, Int) -> Int
): String{
return "The answer is \${operator(num1, num2)}."
}
``````

``````textView.text= calculate(5, 7, plus)
``````

`The answer is 12.`

``````val minus = fun (num1: Int, num2: Int): Int = num1- num2
``````

``````val ans1= calculate(5, 3, plus)
val ans2= calculate(5, 3, minus)
``````

## Lambda Function

Lambda function 可以將 function 簡化，且被大括號所包覆。

`{參數名稱: 參數型態 -> 方法內容}`

``````val plus= { num1: Int, num2: Int ->
num1+ num2
}
``````

``````val plus: (Int, Int) -> Int= { num1, num2 ->
num1+ num2
}
``````

``````btn.setOnClickListener {
TODO("Not yet implemented")
}
``````

``````interface MyClickListener : View.OnClickListener {
override fun onClick(p0: View?)
fun otherFunction()
}
``````

``````btn.setOnClickListener(object : MyClickListener{
override fun onClick(p0: View?) {
TODO("Not yet implemented")
}
override fun otherFunction() {
}
})
``````