Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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) {
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) {
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


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Kotlin: functions

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×