Menu

Kotlin: Function or Methods Example

Liked this Post?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on reddit
Share on Reddit
Share on linkedin
Share on LinkedIn
Share on whatsapp
Share on WhatsApp
Share on telegram
Share on Telegram
  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"}
        }
    }

     

About the Author

Edge Developer

Edge Developer

Hello there, my name is Opeyemi Olorunleke. I am a Software Developer (majorly Android, GitHub Profile), Digital Marketer, Udemy Instructor, Technical Writer, Blogger & Webmaster.

Related Posts

Leave a Comment

Your email address will not be published. Required fields are marked *