{"id":1046,"date":"2024-11-10T00:23:31","date_gmt":"2024-11-10T00:23:31","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=1046"},"modified":"2024-11-10T00:25:36","modified_gmt":"2024-11-10T00:25:36","slug":"kotlin-function","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2024\/11\/10\/kotlin-function\/","title":{"rendered":"[Kotlin]\ud568\uc218\/function"},"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.function1\n\nimport android.os.Bundle\nimport android.widget.Button\nimport android.widget.EditText\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\n    \/\/ \ubdf0\ubc14\uc778\ub529\uc744 \uc0ac\uc6a9\ud558\uc9c0 \uc54a\ub294\uacbd\uc6b0\n    \/\/ When not using view binding\n    private lateinit var inputtext1: EditText\n    private lateinit var inputtext2: EditText\n    private lateinit var btn1 : Button\n    private lateinit var btn2 : Button\n    private lateinit var btn3 : Button\n    private lateinit var txtview1: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        \/\/-----------------------------------------\n        btn1 = findViewById&lt;Button&gt;(R.id.button)\n        btn2 = findViewById&lt;Button&gt;(R.id.button2)\n        btn3 = findViewById&lt;Button&gt;(R.id.button3)\n        inputtext1 = findViewById&lt;EditText&gt;(R.id.input1)\n        inputtext2 = findViewById&lt;EditText&gt;(R.id.input2)\n        txtview1 = findViewById&lt;TextView&gt;(R.id.textview1)\n        val firstValue = inputtext1.text\n        val secondValue = inputtext2.text\n\n        btn1.setOnClickListener(){\n            show(\"\ubc84\ud2bc\uc774 \ud074\ub9ad\ub410\uc5b4\uc694!!\\nThe button was clicked!!\")\n        }\n\n        btn2.setOnClickListener(){\n\n            if(firstValue.isEmpty() || secondValue.isEmpty()){\n                txtview1.text = \"\uac12\uc744 \uc785\ub825\ud558\uc138\uc694\\nPlease enter a value\"\n            }else{\n                val firstStr = firstValue.toString()\n                val secondStr = secondValue.toString()\n                val first = firstStr.toInt()\n                val second = secondStr.toInt()\n                val result = add(first ,second)\n                txtview1.text = \"\uacb0\uacfcresult: $result\"\n            }\n\n        }\n        btn3.setOnClickListener(){\n\n            if(firstValue.isEmpty() || secondValue.isEmpty()){\n                txtview1.text = \"\uac12\uc744 \uc785\ub825\ud558\uc138\uc694\\nPlease enter a value\"\n            }else {\n                val result = add(getFirst(), getSecond())\n                txtview1.text = \"\uacb0\uacfcresult: $result\"\n\n                \/* add(a=10, b=10) or ... *\/\n                val result2 = add(a = 10)\n                var result3 = sum(10, 20, 30)\n            }\n        }\n\n    } \/\/ End onCreate function\n\n    \/\/ vararg : variable arguments\n    \/\/ \uc5ec\ub7ec\uac1c\uc758 \ud568\uc218 \ub9e4\uac1c\ubcc0\uc218(\uc778\uc218&#91;argument])\ub97c \uc4f8 \uc218 \uc788\uc74c.\n    \/\/ Multiple function arguments can be used.\n    fun sum(vararg inputs:Int):Int{\n        var output = 0\n        for(num in inputs){\n            output = output + num\n        }\n        return output\n    }\n    fun getFirst():Int{\n        val firstStr = inputtext1.text.toString()\n        return firstStr.toInt()\n    }\n    fun getSecond():Int{\n        val secondStr = inputtext2.text.toString()\n        return secondStr.toInt()\n    }\n\n    \/\/ :Int\ub294 \ub9ac\ud134\uc758 \ubcc0\uc218 \ud615\n    \/\/ :Int is the return variable type.\n    fun add(a:Int,b:Int=0):Int {\n        return a + b\n    }\n    \/\/ \ud568\uc218\ub294 onCreate\ud568\uc218 \ubc16\uc5d0\uc11c \ub9cc\ub4e4\uac83\n    \/\/ You must create the function outside of the onCreate function.\n    fun show(message:String?){\n        println(message)\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=\"432\" height=\"754\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/11\/function1.png\" alt=\"\" class=\"wp-image-1047\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/11\/function1.png 432w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2024\/11\/function1-172x300.png 172w\" sizes=\"auto, (max-width: 432px) 100vw, 432px\" \/><\/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\/textview1\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_marginTop=\"36dp\"\n        android:text=\"\uacb0\uacfcresult\"\n        android:textSize=\"20sp\"\n        app:layout_constraintBottom_toBottomOf=\"parent\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintHorizontal_bias=\"0.495\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/button3\"\n        app:layout_constraintVertical_bias=\"0.057\" \/&gt;\n\n    &lt;EditText\n        android:id=\"@+id\/input2\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_marginTop=\"24dp\"\n        android:ems=\"10\"\n        android:inputType=\"numberSigned\"\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\/input1\" \/&gt;\n\n    &lt;EditText\n        android:id=\"@+id\/input1\"\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:layout_marginTop=\"60dp\"\n        android:ems=\"10\"\n        android:inputType=\"numberSigned\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toTopOf=\"parent\" \/&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=\"28dp\"\n        android:text=\"\ubcf4\uc5ec\uc8fc\uae30Show\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/input2\" \/&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=\"28dp\"\n        android:text=\"\ub354\ud558\uae30Plus\"\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=\"28dp\"\n        android:text=\"\ub354\ud558\uae30\ud568\uc218Plus function\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintHorizontal_bias=\"0.498\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/button2\" \/&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-1046","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\/1046","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=1046"}],"version-history":[{"count":2,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/1046\/revisions"}],"predecessor-version":[{"id":1050,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/1046\/revisions\/1050"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=1046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=1046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=1046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}