Kotlin Programming – Functions

In this tutorial you’ll learn about functions in Kotlin. To follow along, you can make use of the Kotlin – Playground.

Functions

A function is written to perform a specific task. Instead of writing the same piece of codes multiple times, you use a function to contain it and then you can call the function countless times you want. For example, let’s say you want to perform sum of a list of integers, you can write a function with codes specific to that. You define a function with the keyword fun, followed by the name of the function calculateSum taking one parameter, a list of integers, which returns an Int(known as a return type) as the computational value.

fun calculateSum(numbers: List<Int>) : Int {
    var sum = 0
 
    for (number in numbers) {
        sum+=number
    }
 
    return sum
}
 
println(calculateSum(listOf(2, 4, 6, 8, 10)))

A function can take one or more parameters or no parameters. It can return a value as a result or no value.

Let’s see some examples of functions with no parameter and no return type.

// function with no parameter which returns a String
 
fun getDefaultText() : String {
    return "Hello World!"
}
 
println(getDefaultText())
 
// function with one parameter a String which has no return type
 
fun getText(name: String) {
    println("Hello $name")
}
 
getText("Zakhia")

If you have a single expression inside a function, you can omit the curly braces and the return keyword. You can then use the equal (=) symbol to the expression. In our previous example, we defined the function getDefaultText() which returns a single expression. Let’s define another function using the single-expression function.

fun getQuote() : String = "Most of the important things in the world have been accomplished by people who have kept on trying when there seemed to be no help at all. ~ Dale Carnegie"

Default Argument

You can provide a default value to the parameter so that you can omit the parameter name in the function call.

fun greetUser(name: String = "there") : String = "Hi $name!"
 
println(greetUser())
println(greetUser("Zakhia"))

Named Argument

Suppose you want to define a function with some parameters having default values. You are not required to provide values to the parameters in the same order you defined them in the function. However, you are required to provide the named argument so that it is clear to Kotlin for which argument your value is for. Let’s try an example to better understand this concept.

fun getFullName(first: String = "", middle: String = "", last: String = "") : String = first + " " + middle + " " + last

println(getFullName("Jane"))
println(getFullName("Jane", last = "Doe"))
println(getFullName("Jose", last = "Dagobah", middle = "Arnel"))

Variable Number of Arguments (Varargs)

You can use a vararg parameter as an alternative way of passing a list as a parameter in a function. Recall our function calculateSum(numbers:), instead of making the parameter type a list of integers, we can use a vararg parameter of type Int like this vararg numbers: Int.

fun calculateSum(vararg numbers: Int) : Int {
    var sum = 0
 
    for (number in numbers) {
        sum+=number
    }
 
    return sum
}
 
println(calculateSum(1, 2, 3, 4, 5, 6))

Reference

https://kotlinlang.org/docs/reference/functions.html