Saturday, 3 February 2018

Kotlin: Member functions

A function that is defined inside a class (or) object is called member function.

Ex
class Number {

         fun isEven(num: Int): Boolean {
                 return num % 2 == 0
         }
}

In above snippet, isEven is a member function.

FunctionDemo.kt

class Number {

 fun isEven(num: Int): Boolean {
  return num % 2 == 0
 }
}

fun main(args: Array<String>) {
 var obj = Number()

 var is5Even = obj.isEven(5)
 var is6Even = obj.isEven(6)

 println("Is 5 Even : $is5Even")
 println("Is 6 Even : $is6Even")
}

Output

Is 5 Even : false
Is 6 Even : true


Previous                                                 Next                                                 Home

No comments:

Post a Comment