There are number of diffrent functions in kotlin which varies according to their syntaxes. Let's take them one by one.
fun printMessage(message: String): Unit {
println(message)
}
This is a simple function that takes a parameter of type String
and returns Unit
(In kotlin Unit
is datatype which return no value).
fun printMessageWithPrefix(message: String, prefix: String = "Info") {
println("[$prefix] $message")
}
A function that takes a second optional parameter prefix
with default value Info
. The return type is omitted, meaning that it's actually Unit
.
fun sum(x: Int, y: Int): Int {
return x + y
}
A function that returns an integer
value. this function is also can be written as :
fun sum(x: Int, y: Int) = x + y
A single-expression function that returns an integer
(inferred).
Now, lets write a main
function and call above function frome there :
fun main() {
printMessage("Hello") //1
printMessageWithPrefix("Hello", "Log") //2
printMessageWithPrefix("Hello") //3
printMessageWithPrefix(prefix = "Log", message = "Hello") //4
println(sum(1, 2)) //5
}
1. Calls the first function with the argument Hello
.
2. Calls the function with two parameters, passing values for both of them.
3. Calls the same function omitting the second one. The default value Info
is used.
4. Calls the same function using named arguments and changing the order of the arguments.
5. Prints the result of a function call sum(1, 2)
.
Functions marked with the infix
keyword can also be called using the infix
notation (omitting the dot and the parentheses for the call). Infix functions must satisfy the following requirements:
default
value.infix fun Int.shl(x: Int): Int { ... }
// calling the function using the infix notation
1 shl 2
// is the same as
1.shl(2)
Note that infix functions always require both the receiver and the parameter
to be specified. When you're calling a method on the current receiver using the infix
notation, you need to use this explicitly; unlike regular method calls, it cannot be omitted.
Member functions and extensions with a single parameter can be turned into infix
functions.
infix fun Int.times(str: String) = str.repeat(this) //1
println(2 times "Bye ") //2
1. Defines an infix extension function on Int
.
2. Calls the infix
function.
Above code print Bye Bye
in output console
val pair = "Ferrari" to "Katrina" // 3
println(pair)
Creates a Pair
by calling the infix
function to from the standard library.
infix fun String.onto(other: String) = Pair(this, other) //4
val myPair = "McLaren" onto "Lucas" //5
println(myPair)
Here's your own implementation of to
creatively called onto
.
Certain functions can be "upgraded" to operators
, allowing their calls with the corresponding operator
symbol. Functions that overload operators need to be marked with the operator
modifier as bellow :
operator fun Int.times(str: String) = str.repeat(this) // 1
println(2 * "Bye ")
This takes the infix
function from above one step further using the operator
modifier. The operator
symbol for times()
is *
so that you can call the function using 2 * "Bye"
.
operator fun String.get(range: IntRange) = substring(range)
val str = "Always forgive your enemies; nothing annoys them so much."
println(str[0..14])
An operator
function allows easy range
access on strings
. The get()
operator enables bracket-access syntax. Here, output will be generated by substring
from 0 to 14 as Always forgive
from given str
string.