skip to content

Functions

Everything about declaring and calling functions in Kotlin: top-level and local functions, default and named arguments, varargs, extensions, the scope-function family, and special forms like infix and operator. Idiomatic Kotlin lives or dies on this material.

part ofKotlinoverview, primer and where to startread it →
on this pageshow

explore

questions

120 · 5 sections

What does it mean that functions are 'first-class' in Kotlin, and how does that differ from how Java traditionally modeled functions?

level: juniorimportance: must knowfreq 70%
basics
~20 s

First-class means a function is a value: you can store it in a variable, pass it to another function, and return it. In Kotlin you do this directly; old Java needed an interface or anonymous class wrapper.

open as a page

How do extension functions fit the Kotlin function model, and what does the receiver let you do that a plain top-level function does not? What are the key limits?

level: middleimportance: must knowfreq 65%
basics
~20 s

An extension function looks like a method added to an existing type. You write fun String.shout() = uppercase() + "!" and call "hi".shout(). It is really a static function with the receiver as a hidden first argument; it cannot access private members and is resolved statically.

open as a page

Explain named and default arguments in Kotlin. How do they reduce overloads, and what interop/ordering rules must you keep in mind?

level: middleimportance: must knowfreq 60%
basics
~20 s

A default argument gives a parameter a value used when the caller omits it. A named argument lets the caller pass values by parameter name instead of position. Together they remove the need for many overloaded versions of the same function.

open as a page

Where can a Kotlin function be declared (top-level, member, local), and what is the practical reason to choose a top-level function over a static helper class?

level: middleimportance: should knowfreq 55%
basics
~20 s

You can declare a function directly in a file (top-level), inside a class (member), or inside another function (local). Top-level functions need no wrapper class, so they replace Java's 'Utils' classes full of static methods.

open as a page

How do `operator` and `infix` functions extend the function model in Kotlin? Give the rules and a concrete example of each.

level: seniorimportance: should knowfreq 45%
basics
~20 s

operator lets a function back a symbol like +, [], or in, so a + b calls a.plus(b). infix lets you call a one-parameter function without the dot and parentheses, like 1 to 2. Both are syntax sugar over normal functions.

open as a page

What are default arguments in Kotlin, and how do they let you avoid writing multiple overloads?

level: juniorimportance: must knowfreq 80%
basics
~10 s

A default argument gives a parameter a value used when the caller omits it. So one function with defaults replaces many overloaded functions that differ only by which parameters they accept.

open as a page

What is a local (nested) function in Kotlin, and what is the main reason to use one instead of a private top-level or member function?

level: juniorimportance: must knowfreq 55%
basics
~20 s

A local function is a function declared inside another function. The main reason to use one is to reuse a small piece of logic that only matters to the surrounding function, while reading its local variables directly.

open as a page

What are named arguments in Kotlin, and what call-site problems do they solve?

level: juniorimportance: must knowfreq 70%
basics
~10 s

Named arguments let you pass a value by writing the parameter's name, like draw(width = 5). This makes calls easier to read and lets you supply only the arguments you want, in any order.

open as a page

What does the :: operator do when applied to a function name, e.g. `::println` or `String::length`, and what is the resulting value?

level: juniorimportance: must knowfreq 70%
basics
~20 s

The :: operator turns a named function into a value you can store in a variable or pass to another function, instead of calling it. The result is a function object you can invoke later.

open as a page

What is a single-expression function in Kotlin, and how does its syntax differ from a regular block-body function?

level: juniorimportance: must knowfreq 70%
basics
~20 s

It is a function whose body is a single expression written after an equals sign instead of curly braces. For example: fun square(x: Int) = x * x. The result of the expression is what the function returns.

open as a page

In Kotlin, when you call an extension function, what determines which function actually runs: the variable's declared type or the actual object stored in it?

level: juniorimportance: must knowfreq 70%
basics
~10 s

The variable's declared type decides. Kotlin picks the extension function at compile time based on how the variable is typed, not on the real object it holds at runtime.

open as a page

What is an extension function in Kotlin, and how do you declare one?

level: juniorimportance: must knowfreq 85%
basics
~20 s

An extension function lets you add a new function to an existing type without changing its source. You write the type name before the function name, like fun String.shout(), and call it as if it were a normal method.

open as a page

What is a Kotlin extension property, and how do you declare one (e.g. a `lastIndex` for `String`)?

level: juniorimportance: must knowfreq 60%
basics
~10 s

An extension property lets you add a property to an existing type without changing its code. You write it like val String.lastIndex with a custom getter that computes the value.

open as a page

You declared an extension function in a different file/package than where you want to call it. How do you make it usable, and why is this different from member functions?

level: juniorimportance: must knowfreq 70%
basics
~20 s

An extension defined elsewhere is only visible if you import it by name. Add an import line for that function, then call it like normal. Member functions come for free with the object's type, so they need no import.

open as a page

In Kotlin, if a class has a member function and you also write an extension function with the exact same name and parameter signature, which one gets called when you invoke it on an instance of that class?

level: juniorimportance: must knowfreq 70%
basics
~10 s

The member function always wins. When the name and parameters match, Kotlin calls the function defined inside the class and ignores your extension. The extension is effectively shadowed.

open as a page

What do Kotlin's apply { } and also { } return, and how does each give you access to the object inside the lambda?

level: juniorimportance: must knowfreq 80%
basics
~10 s

Both apply and also return the same object you called them on. Inside apply you use it as this; inside also you use it as it. Neither changes what value comes out.

open as a page

What does Kotlin's let scope function do, and how do you access the value inside its lambda?

level: juniorimportance: must knowfreq 80%
basics
~10 s

let runs a block of code on a value. Inside the block the value is called it, and let returns whatever the last line of the block produces.

open as a page

What do Kotlin's run { } and with(x) { } scope functions do, and what does each return?

level: juniorimportance: must knowfreq 70%
basics
~20 s

Both run a block of code on an object you can call this on. run is called on the object; with takes the object as an argument. Both return the last expression in the block.

open as a page

What does the expression `name?.let { it.uppercase() } ?: "UNKNOWN"` do, and how does it handle a null `name`?

level: juniorimportance: must knowfreq 80%
basics
~10 s

If name is not null, it runs the block and uppercases it. If name is null, the block is skipped and the whole expression becomes "UNKNOWN". The Elvis operator (?:) supplies the fallback.

open as a page

What are the two axes that distinguish Kotlin's five scope functions (let, run, with, apply, also), and how do they map to choosing one?

level: juniorimportance: must knowfreq 75%
basics
~20 s

Two things differ: how you refer to the object inside the block (as 'this' or as 'it'), and what the block gives back (the block's last line, or the original object). Pick based on those two needs.

open as a page

What is an infix function in Kotlin, and what are the requirements a function must satisfy to be callable in infix form?

level: juniorimportance: must knowfreq 55%
basics
~10 s

An infix function lets you call it between two values without a dot or parentheses, like 1 to 2. It must be marked infix, be a member or extension, and take exactly one parameter.

open as a page

What does the `invoke` operator function do in Kotlin, and how do you make an instance of your own class callable like a function?

level: juniorimportance: must knowfreq 55%
basics
~10 s

Declaring operator fun invoke(...) on a class lets you call an instance with parentheses, like obj(), instead of writing obj.invoke(). It turns an object into something you can call like a function.

open as a page

What is operator overloading in Kotlin, and how do you make the `+` symbol work on your own class?

level: juniorimportance: must knowfreq 70%
basics
~20 s

Operator overloading lets symbols like + or [] work on your own types. You write a function with a fixed name (like plus) and mark it with the operator keyword. Then a + b calls a.plus(b).

open as a page

How does the standard-library `to` function work, and how do `until`, `downTo`, and `step` use infix to make range code read naturally?

level: middleimportance: must knowfreq 50%
basics
~20 s

to is an infix extension that builds a Pair, so "a" to 1 makes Pair("a", 1). until, downTo, and step are infix functions that build ranges/progressions, letting you write 0 until n or 10 downTo 1 step 2.

open as a page

How do indexing (`a[i]`) and call (`a()`) syntax map to functions via operator overloading? Show `get`, `set`, and `invoke`.

level: middleimportance: must knowfreq 60%
basics
~10 s

a[i] calls a.get(i), and a[i] = v calls a.set(i, v). a() calls a.invoke(). Each must be declared with the operator keyword.

open as a page