Friday 26 January 2018

Kotlin: functions

Function is a block of statements, used to reuse the code. In kotlin, there are two types of funcitons.
a.   Kotlin library functions (ex: print, sqrt etc.,)
b.   User defined functions.

In this tutorial series, I am going to focus on user defined functions.

In Kotlin, functions are defined using 'fun' keyword.

Syntax
fun functionName(parameters) : returnType{

}

If you do not specify a returnType, it treats it as a void method. parameters are optional.

Parameters must be typed and multiple parameters are separated by comma (,).

Syntax
fun functionName(param1 : type, param2: type …. paramN:type) : returnType{

}

Ex
// void function with no arguments
fun printWelcomeMessage() {
         println("Welcome buddy")
}

// void function with one argument
fun printWelcomeMessage(name: String) {
         println("Welcome $name")
}

// void method with two arguments
fun printWelcomeMessage(firstName: String, lastName: String) {
         println("Welcome [$firstName, $lastName]")
}

//non-void function with no arguments
fun getWelcomeMessage(): String {
         return "Welcome"
}

//non-void function with one argument
fun getWelcomeMessage(name: String): String {
         return "Welcome $name"
}

//non-void function with two arguments
fun getWelcomeMessage(firstName: String, lastName: String): String {
         return "Welcome [$firstName, $lastName]"
}
  

FunctionsDemo.kt

// void function with no arguments
fun printWelcomeMessage() {
 println("Welcome buddy")
}

// void function with one argument
fun printWelcomeMessage(name: String) {
 println("Welcome $name")
}

// void method with two arguments
fun printWelcomeMessage(firstName: String, lastName: String) {
 println("Welcome [$firstName, $lastName]")
}

//non-void function with no arguments
fun getWelcomeMessage(): String {
 return "Welcome"
}

//non-void function with one argument
fun getWelcomeMessage(name: String): String {
 return "Welcome $name"
}

//non-void function with two arguments
fun getWelcomeMessage(firstName: String, lastName: String): String {
 return "Welcome [$firstName, $lastName]"
}


fun main(args: Array<String>) {
 printWelcomeMessage()

 printWelcomeMessage("Ram")

 printWelcomeMessage("Ram", "Krishna")

 var message = getWelcomeMessage()
 println("${message}")

 message = getWelcomeMessage("Ram")
 println("${message}")

 message = getWelcomeMessage("Ram", "Krishna")
 println("${message}")
}

Output

Welcome buddy
Welcome Ram
Welcome [Ram, Krishna]
Welcome
Welcome Ram
Welcome [Ram, Krishna]

If your function body has only one statement, then you can omit the braces {}

Ex
fun square(x: Int) = x * x
fun cube(x: Int) = x * x * x
fun printMessage() = "Hello World"


FunctionsDemo.kt

fun square(x: Int) = x * x
fun cube(x: Int) = x * x * x
fun printMessage() = "Hello World"

fun main(args: Array<String>) {
 println("Square of 10 is ${square(10)}")
 println("Cube of 10 is ${cube(10)}")
 println(printMessage())
}

Output

Square of 10 is 100
Cube of 10 is 1000
Hello World



Previous                                                 Next                                                 Home

No comments:

Post a Comment