{"id":1052,"date":"2024-11-11T10:23:08","date_gmt":"2024-11-11T10:23:08","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=1052"},"modified":"2024-11-11T10:25:44","modified_gmt":"2024-11-11T10:25:44","slug":"kotline-function2","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2024\/11\/11\/kotline-function2\/","title":{"rendered":"[Kotline]\ud568\uc2182\/function2"},"content":{"rendered":"\n<p>\ub354 \uce5c\uc808\ud55c \ucf54\ud2c0\ub9b0 \uc571\ud504\ub85c\uadf8\ub798\ubc0d \ucc45\uc5d0 \uc788\ub294 \ucf54\ub4dc \uc785\ub2c8\ub2e4.<br>This is code from the Friendlier Kotlin App Programming book<\/p>\n\n\n\n<p>\ud568\uc218 \uc608\uc81c\uc785\ub2c8\ub2e4.<br>This is an example of function.<\/p>\n\n\n\n<p>\uc774 \ucf54\ub4dc\ub294 \ubdf0\ubc14\uc778\ub529\uc744 \uc0ac\uc6a9\ud558\uc9c0 \uc54a\uc558\uc2b5\ub2c8\ub2e4.<br>This code does not use view binding.<\/p>\n\n\n\n<p>Test : compileSdk = 34 targetSdk = 34<\/p>\n\n\n\n<p>MainActivity.kt<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.example.funtion2\n\nimport android.os.Bundle\nimport android.widget.Button\nimport android.widget.TextView\nimport androidx.activity.enableEdgeToEdge\nimport androidx.appcompat.app.AppCompatActivity\nimport androidx.core.view.ViewCompat\nimport androidx.core.view.WindowInsetsCompat\n\nclass MainActivity : AppCompatActivity() {\n    private lateinit var button: Button\n    private lateinit var button2:Button\n    private lateinit var button3:Button\n    private lateinit var button4:Button\n    private lateinit var button5:Button\n    private lateinit var output1: TextView\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        enableEdgeToEdge()\n        setContentView(R.layout.activity_main)\n        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -&gt;\n            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())\n            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)\n            insets\n        }\n     \n        button = findViewById(R.id.button)\n        button2 = findViewById(R.id.button2)\n        button3 = findViewById(R.id.button3)\n        button4 = findViewById(R.id.button4)\n        button5 = findViewById(R.id.button5)\n        output1 = findViewById(R.id.output1)\n\n        button.setOnClickListener() {\n           \n            val addFunc = { a: Int, b: Int -&gt; a + b }\n            val result = addFunc(10,10)\n            output1.text = \"\uacb0\uacfcresult: $result\"\n\n        }\n        button2.setOnClickListener {\n            val addFunc = { a: Int, b: Int -&gt; a + b }\n            val result = calc(30, 10, addFunc)\n            output1.text = \"\uacb0\uacfcresult: $result\"\n\n        }\n\n        button3.setOnClickListener {\n            \/\/ \ud568\uc218\ub97c \ub9ac\ud134\ud558\ub294 \ud568\uc218\n            \/\/ a function that returns a function\n            val oper = getOperator(\"subtract\")\n            if(oper != null){\n                val result = calc(30,10,oper)\n                output1.text = \"\uacb0\uacfcresult: $result\"\n\n            }\n        }\n        button4.setOnClickListener {\n            \/\/ \ub78c\ub2e4\uc2dd\uc758 \ub2e4\ub978 \ud615\ud0dc\n            \/\/ Other forms of lambda expressions\n            val multiply = { a:Int,b:Int -&gt; a + b }\n            val multiply2:( Int,Int)-&gt;Int = {a, b -&gt; a + b}\n            val multiply3:(Int,Int)-&gt;Int = {a, b -&gt; a * b}\n\n            val result = calc(30,10,multiply3)\n            output1.text = \"\uacb0\uacfcresult: ${result}\"\n        }\n\n        button5.setOnClickListener {\n            val sum = fun(a:Int,b:Int):Int{\n                var result = 0\n                result = a + b\n                return result\n            }\n\n            val result2 = sum(10,20)\n            output1.text= \"\uacb0\uacfcresult:${result2}\"\n\n\n            \/\/\uccab\ubc88\uca30 \ud615\ud0dc\n            \/\/ First form\n            doAction(fun ():Int {\n                println(\"\uc804\ub2ec\ub41c \ud568\uc218 \ud638\ucd9c\ub428 Passed function called\")\n                return 10\n            })\n\n            \/\/\ub450\ubc88\uc9f8 \ud615\ud0dc:\uac04\uc18c\ud654\ub41c \ud615\ud0dc\n            \/\/ second form\n            doAction {\n                println(\"\uc804\ub2ec\ub41c \ud568\uc218 \ud638\ucd9c\ub428 Passed function called\")\n                10\n            }\n\n            \/\/ \uc138\ubc88\uca30 \ud615\ud0dc\n            \/\/ third form\n            doAction {\n                println(\"\uc804\ub2ec\ub41c \ud568\uc218 \ud638\ucd9c\ub428 Passed function called\")\n                return@doAction 10\n            }\n        }\n\n    }\n\n    fun doAction(action:()-&gt;Int){\n        println(\"doAction\uc2e4\ud589\")\n        val result = action()\n    }\n\n    fun show():Unit{ \n        println(\"show\ud568\uc218 \ud638\ucd9c\ub428\")\n    }\n\n    fun getOperator(name: String): ((Int, Int) -&gt; Int)? {\n        var oper: ((Int, Int) -&gt; Int)? = null\n\n        if (name == \"add\") {\n            oper = { a: Int, b: Int -&gt; a + b }\n        } else if (name == \"subtract\") {\n            oper = { a:Int,b:Int -&gt; a - b }\n        }\n        return oper\n    }\n\n    fun calc(first:Int,second:Int,oper:(Int,Int)-&gt;Int):Int{\n        return oper(first,second)\n    }\n\n}<\/code><\/pre>\n\n\n\n<p>active_main.xml<\/p>\n\n\n\n<p>-xml design<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"430\" height=\"749\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/11\/function2.png\" alt=\"\" class=\"wp-image-1053\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/11\/function2.png 430w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/11\/function2-172x300.png 172w\" sizes=\"auto, (max-width: 430px) 100vw, 430px\" \/><\/figure>\n\n\n\n<p>-xml code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:id=\"@+id\/main\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    tools:context=\".MainActivity\"&gt;\n\n    &lt;TextView\n        android:id=\"@+id\/output1\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_marginTop=\"48dp\"\n        android:text=\"\ub354\ud558\uae30\uacb0\uacfcPlus Result\"\n        android:textSize=\"20sp\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/button5\" \/&gt;\n\n    &lt;Button\n        android:id=\"@+id\/button\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_marginTop=\"64dp\"\n        android:text=\"\ud568\uc218\uc2e4\ud589 Function execution\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintHorizontal_bias=\"0.497\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toTopOf=\"parent\" \/&gt;\n\n    &lt;Button\n        android:id=\"@+id\/button2\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_marginTop=\"40dp\"\n        android:text=\"\ud568\uc218\uc2e4\ud5892 Function execution2\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/button\" \/&gt;\n\n    &lt;Button\n        android:id=\"@+id\/button3\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_marginTop=\"32dp\"\n        android:text=\"\ud568\uc218\uc2e4\ud5893 Function execution3\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/button2\" \/&gt;\n\n    &lt;Button\n        android:id=\"@+id\/button4\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_marginTop=\"36dp\"\n        android:text=\"\ud568\uc218\uc2e4\ud5894 Function execution4\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintHorizontal_bias=\"0.502\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/button3\" \/&gt;\n\n    &lt;Button\n        android:id=\"@+id\/button5\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_marginTop=\"40dp\"\n        android:text=\"\ud568\uc218\uc2e4\ud5895 Function execution5\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintHorizontal_bias=\"0.497\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/button4\" \/&gt;\n\n&lt;\/androidx.constraintlayout.widget.ConstraintLayout&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ub354 \uce5c\uc808\ud55c \ucf54\ud2c0\ub9b0 \uc571\ud504\ub85c\uadf8\ub798\ubc0d \ucc45\uc5d0 \uc788\ub294 \ucf54\ub4dc \uc785\ub2c8\ub2e4.This is code from the Friendlier Kotlin App Programming book \ud568\uc218 \uc608\uc81c\uc785\ub2c8\ub2e4.This is an example of function. \uc774 \ucf54\ub4dc\ub294 \ubdf0\ubc14\uc778\ub529\uc744 \uc0ac\uc6a9\ud558\uc9c0 \uc54a\uc558\uc2b5\ub2c8\ub2e4.This code does not use view binding. Test : compileSdk = 34 targetSdk = 34 MainActivity.kt active_main.xml -xml design -xml code<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,1],"tags":[],"class_list":["post-1052","post","type-post","status-publish","format-standard","hentry","category-kotlin","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/1052","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=1052"}],"version-history":[{"count":2,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/1052\/revisions"}],"predecessor-version":[{"id":1056,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/1052\/revisions\/1056"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=1052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=1052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=1052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}