Kotlin Methods Example

  1. Simple Function. neither accepts nor return any variable or value
    fun helloWorld(){
      println("Hello World")
    }
  2.  Function that takes a parameter but returns no value
    fun stringFunction (text : String){
     for (c in text){
       println("$c ") //KOTLIN -> K O T L I N
     }
    }
    
    fun intFunction(int : Integer){
      println(int)
    }
  3. Functions that returns a value but takes no parameter
    fun getAuthorsName() : String {
     return "Olorunleke Opeyemi"
    }
    
    fun getTodaysDate() : Date {
      return Date()
    }
  4. Functions that accept a value and returns a value
    class Example{
     fun main(args: Array<String>) {
       println(evaluateInteger(21, 23))
     }
      fun evaluateInteger (a : Int, b: Int) : String {
       return if (a>b) {
               "$a is greater than $b"
            } else if (a<b){
               "$b is greater than $a"
            } else {"they are equal"}
        }
    }
     

Comments

Popular posts from this blog

How to select Multiple Item in RecyclerView in Android

Android Clipboard Listener - How to Know when User copies a text in Android

Kotlin ArrayList and Loops Example