[Kotline]ํ•จ์ˆ˜2/function2

๋” ์นœ์ ˆํ•œ ์ฝ”ํ‹€๋ฆฐ ์•ฑํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ฑ…์— ์žˆ๋Š” ์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค.
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>

Leave a Reply