T E C h O C E A N H U B

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.

Copyright ©TechOceanhub All Rights Reserved.