Techoceanhub logo

Let’s learn how to create a method in kotlin-Chapter-4

In the previous post, we made a HelloWorld program in Kotlin, that was quite simple but today we will see how to make a simple method or function in Kotlin. 

Basically, the declaration of function starts with a fun keyword. The simple syntax of function will look like below:-

             fun addMethod(){

             }

Kotlin takes a return type of a method in a simple manner like:-

            fun addMethod():Int{

            }

Now let us see a simple code that explains the creation of a simple method in kotlin.

package adapter.manasvi.com.simpleproject

import android.app.Application
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class HelloWorld : AppCompatActivity() {

    val str:String=”Hello World”

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        someMethod()
    }

    fun someMethod() {
        textView.text=str
    }

}


Feel free to read and try the above example.

Share:

Categories

Archives

Related Posts

Array Destructuring

Array Destructuring Array destructuring is a powerful syntax introduced in ES6 that allows you to unpack elements from an array

Read More »

Explore our Affiliated partner links

hostgator website plan

Hostgator

Best Website hosting plans & Best for 24/7 support.

hostinger webhosting plan

Hostinger

Reasonable website hosting packages for small business & 24/7 Email support

Elementor pro

Design Worpdress website quickly using Elementor Builder and AI

buy elementor website templates

Buy Elementor Website Template

Buy ready made Elementor Website Template from us & Get Free Elementor pro licence for 1 year .