Defining and Calling Functions

Keywords

ver. 1.0.0

Make Kotlin functions easy to call, place, and extend — including top-level and extension functions, defaults, varargs, infix notation, string utilities, and local helpers.

This unit teaches how to call, organize, and extend Kotlin functions so they read well and interoperate with Java. You will learn Kotlin collections’ underlying Java classes, named arguments, default parameter values, top-level functions and their JVM representation, extension functions and properties (and why they’re not overridable), vararg and spread usage, infix calls with destructuring, Kotlin string utilities and triple-quoted strings for readable regexes, and how to extract local (nested) functions to remove duplication without polluting the namespace.

By the end of this unit you’ll be able to write and use Kotlin functions in ways that make call sites clear, place functionality where it belongs, and add behavior to types you don’t own — all while keeping Java interoperability in mind.

Key capabilities you’ll gain: - Create collections with functions like setOf, listOf, hashMapOf and recognize that Kotlin collections are implemented as Java collection classes (e.g., java.util.ArrayList), reflecting Kotlin’s strong Java interoperability. - Improve call-site readability by using named arguments, and drastically simplify APIs by giving parameters default values instead of creating many overloads. - Declare functions and properties at the top level of a file. Understand that the compiler still generates a JVM class for the file and how annotations like @JvmName control that emitted class name. - Add methods to classes you don’t own using extension functions (receiver syntax such as fun String.lastChar()). Understand their implementation as static functions with the receiver as the first parameter, why they can’t access private members, and why extension functions are not overridable. - Define extension properties with custom accessors where appropriate. - Write vararg functions to accept any number of arguments and use the spread operator (*) to pass an existing array to a vararg parameter. - Use infix notation for single-argument functions (e.g., 1 to “one”) and destructure results returned as pairs or data classes. - Prefer Kotlin’s string extension APIs (which clarify behavior vs. Java’s regex-based overloads) and use triple-quoted strings to write regexes and other literal content cleanly without escaping. - Remove duplicated logic with local (nested) functions so helper code stays scoped to the function that needs it, avoiding accidental exposure in the wider namespace.

The running example of rebuilding joinToString demonstrates how each feature improves readability, placement, interoperability, and reuse. After this unit you will know how to make function APIs pleasant to call, where to put functions, and how to extend existing types safely and idiomatically; classes and objects are covered next to show how declarations map to the Java output the Kotlin compiler produces.

Materials

Source document

  • Kotlin in Action, Second Edition, Sebastian Aigner, Roman Elizarov, Svetlana Isakova, and Dmitry Jemerov, Manning, April 2024 — Link — Page 51-77