๋ ์น์ ํ ์ฝํ๋ฆฐ ์ฑํ๋ก๊ทธ๋๋ฐ ์ฑ
์ ์๋ ์ฝ๋ ์
๋๋ค.
This is code from the Friendlier Kotlin App Programming book
ํจ์ ์์ ์
๋๋ค.
This is an example of function.
์ด ์ฝ๋๋ ๋ทฐ๋ฐ์ธ๋ฉ์ ์ฌ์ฉํ์ง ์์์ต๋๋ค.
This code does not use view binding.
Test : compileSdk = 34 targetSdk = 34
MainActivity.kt
package com.example.funtion2
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class MainActivity : AppCompatActivity() {
private lateinit var button: Button
private lateinit var button2:Button
private lateinit var button3:Button
private lateinit var button4:Button
private lateinit var button5:Button
private lateinit var output1: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
button = findViewById(R.id.button)
button2 = findViewById(R.id.button2)
button3 = findViewById(R.id.button3)
button4 = findViewById(R.id.button4)
button5 = findViewById(R.id.button5)
output1 = findViewById(R.id.output1)
button.setOnClickListener() {
val addFunc = { a: Int, b: Int -> a + b }
val result = addFunc(10,10)
output1.text = "๊ฒฐ๊ณผresult: $result"
}
button2.setOnClickListener {
val addFunc = { a: Int, b: Int -> a + b }
val result = calc(30, 10, addFunc)
output1.text = "๊ฒฐ๊ณผresult: $result"
}
button3.setOnClickListener {
// ํจ์๋ฅผ ๋ฆฌํดํ๋ ํจ์
// a function that returns a function
val oper = getOperator("subtract")
if(oper != null){
val result = calc(30,10,oper)
output1.text = "๊ฒฐ๊ณผresult: $result"
}
}
button4.setOnClickListener {
// ๋๋ค์์ ๋ค๋ฅธ ํํ
// Other forms of lambda expressions
val multiply = { a:Int,b:Int -> a + b }
val multiply2:( Int,Int)->Int = {a, b -> a + b}
val multiply3:(Int,Int)->Int = {a, b -> a * b}
val result = calc(30,10,multiply3)
output1.text = "๊ฒฐ๊ณผresult: ${result}"
}
button5.setOnClickListener {
val sum = fun(a:Int,b:Int):Int{
var result = 0
result = a + b
return result
}
val result2 = sum(10,20)
output1.text= "๊ฒฐ๊ณผresult:${result2}"
//์ฒซ๋ฒ์จฐ ํํ
// First form
doAction(fun ():Int {
println("์ ๋ฌ๋ ํจ์ ํธ์ถ๋จ Passed function called")
return 10
})
//๋๋ฒ์งธ ํํ:๊ฐ์ํ๋ ํํ
// second form
doAction {
println("์ ๋ฌ๋ ํจ์ ํธ์ถ๋จ Passed function called")
10
}
// ์ธ๋ฒ์จฐ ํํ
// third form
doAction {
println("์ ๋ฌ๋ ํจ์ ํธ์ถ๋จ Passed function called")
return@doAction 10
}
}
}
fun doAction(action:()->Int){
println("doAction์คํ")
val result = action()
}
fun show():Unit{
println("showํจ์ ํธ์ถ๋จ")
}
fun getOperator(name: String): ((Int, Int) -> Int)? {
var oper: ((Int, Int) -> Int)? = null
if (name == "add") {
oper = { a: Int, b: Int -> a + b }
} else if (name == "subtract") {
oper = { a:Int,b:Int -> a - b }
}
return oper
}
fun calc(first:Int,second:Int,oper:(Int,Int)->Int):Int{
return oper(first,second)
}
}
active_main.xml
-xml design

-xml code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/output1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="๋ํ๊ธฐ๊ฒฐ๊ณผPlus Result"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button5" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:text="ํจ์์คํ Function execution"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="ํจ์์คํ2 Function execution2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="ํจ์์คํ3 Function execution3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="ํจ์์คํ4 Function execution4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button3" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="ํจ์์คํ5 Function execution5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button4" />
</androidx.constraintlayout.widget.ConstraintLayout>