Functions

A function is a mini-program packaged under a name.

When you call a function (sometimes described as invoking a function), this mini-program is executed. A function combines a group of activities into a single name, and is the most basic way to organize your programs.

Ordinarily, you pass information into a function, and the function uses that information to calculate a result, which it returns to you. The basic form of a function was introduced in Hello, World!:

fun functionName(arg1: Type1, arg2: Type2, ...): ReturnType { lines of code return result }

The arguments are the information you pass into the function, and each one has an identifier name followed by a colon and the type of that argument. The closing parenthesis of the argument list is followed by a colon and the type of result the function produces when you call it. The lines of code in the function body are enclosed in curly braces. The expression following the return keyword is the result the function produces when it’s finished.

Here’s a simple function called multiplyByTwo():

// Functions/MultiplyByTwo.kt fun multiplyByTwo(x: Int): Int { // [1] println("Inside multiplyByTwo") // [2] return x * 2 } fun main(args: Array<String>) { val r = multiplyByTwo(5) // [3] println(r) } /* Output: Inside multiplyByTwo 10 */

All the code inside a function is now executed by a single call, using the function name multiplyByTwo() as an abbreviation for that code. This is why functions are the most basic form of simplification and code reuse in programming. You can also think of a function as an expression with substitutable values (the arguments).

println() is also a function call—it just happens to be a function defined by Kotlin. We refer to such functions as library functions.

If the function doesn’t provide a meaningful result, its return type is considered to be Unit. You can specify this type explicitly if you want, but Kotlin lets you omit it:

// Functions/SayHello.kt fun sayHello() { println("Hallo!") } fun sayGoodbye(): Unit { println("Auf Wiedersehen!") } fun main(args: Array<String>) { sayHello() sayGoodbye() } /* Output: Hallo! Auf Wiedersehen! */

Both sayHello() and sayGoodbye() have the same return type Unit, but the first one leaves out the explicit declaration, which means Unit. The main() function also returns Unit.

If a function only consists of an expression, you can use the abbreviated syntax of an equals sign followed by the expression:

fun functionName(arg1: Type1, arg2: Type2, ...): ReturnType = expression

A function body surrounded by curly braces is called a block body. A function body using the equals syntax is called an expression body.

Here, the multiplyByThree() function uses an expression body:

// Functions/MultiplyByThree.kt fun multiplyByThree(x: Int): Int = x * 3 fun main(args: Array<String>) { println(multiplyByThree(5)) } /* Output: 15 */

This is a short version of saying return x * 3 inside a block body.

With an expression body, the compiler can infer the return type of the function:

// Functions/MultiplyByFour.kt fun multiplyByFour(x: Int) = x * 4 fun main(args: Array<String>) { val result: Int = multiplyByFour(5) println(result) } /* Output: 20 */

Kotlin infers that multiplyByFour() returns an Int.

Kotlin can only infer return types for expression bodies. If a function has a block body and you omit its type, that function returns Unit.

When writing functions, choose descriptive names to make reading the code easier and to reduce the need for code comments. Sometimes, we won’t be as explicit as we would prefer in this book because we’re constrained by line widths.

Previous          Next

©2018 Mindview LLC. All Rights Reserved.