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 pageshowhide
explore
- How Kotlin Does Functions6 questions
- Declaration Forms40 questions
- Top-Level Functions5 questions
- Default Arguments5 questions
- Named Arguments5 questions
- Vararg & Spread Operator5 questions
- Single-Expression Functions5 questions
- Local & Nested Functions5 questions
- Function & Bound References5 questions
- Unit & Nothing Return Types5 questions
- Extension Functions & Properties30 questions
- Extension Functions5 questions
- Extension Properties5 questions
- Nullable & Generic Receivers5 questions
- Dispatch vs Extension Resolution5 questions
- Member-vs-Extension Precedence5 questions
- Extensions Across Files & Interop5 questions
- Scope Functions25 questions
- let5 questions
- run & with5 questions
- apply & also5 questions
- this-vs-it & Choosing One5 questions
- Scope Functions with Null Safety5 questions
- Special Function Forms19 questions
- Infix Functions5 questions
- Operator Overloading5 questions
- invoke Operator5 questions
- tailrec Functions4 questions
questions
120 · 5 sectionsWhat does it mean that functions are 'first-class' in Kotlin, and how does that differ from how Java traditionally modeled functions?
basics
~20 sFirst-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.
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?
basics
~20 sAn 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.
Explain named and default arguments in Kotlin. How do they reduce overloads, and what interop/ordering rules must you keep in mind?
basics
~20 sA 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.
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?
basics
~20 sYou 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.
How do `operator` and `infix` functions extend the function model in Kotlin? Give the rules and a concrete example of each.
basics
~20 soperator 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.
What are default arguments in Kotlin, and how do they let you avoid writing multiple overloads?
basics
~10 sA 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.
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?
basics
~20 sA 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.
What are named arguments in Kotlin, and what call-site problems do they solve?
basics
~10 sNamed 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.
What does the :: operator do when applied to a function name, e.g. `::println` or `String::length`, and what is the resulting value?
basics
~20 sThe :: 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.
What is a single-expression function in Kotlin, and how does its syntax differ from a regular block-body function?
basics
~20 sIt 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.
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?
basics
~10 sThe 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.
What is an extension function in Kotlin, and how do you declare one?
basics
~20 sAn 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.
What is a Kotlin extension property, and how do you declare one (e.g. a `lastIndex` for `String`)?
basics
~10 sAn 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.
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?
basics
~20 sAn 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.
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?
basics
~10 sThe 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.
What do Kotlin's apply { } and also { } return, and how does each give you access to the object inside the lambda?
basics
~10 sBoth 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.
What does Kotlin's let scope function do, and how do you access the value inside its lambda?
basics
~10 slet 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.
What do Kotlin's run { } and with(x) { } scope functions do, and what does each return?
basics
~20 sBoth 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.
What does the expression `name?.let { it.uppercase() } ?: "UNKNOWN"` do, and how does it handle a null `name`?
basics
~10 sIf 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.
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?
basics
~20 sTwo 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.
What is an infix function in Kotlin, and what are the requirements a function must satisfy to be callable in infix form?
basics
~10 sAn 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.
What does the `invoke` operator function do in Kotlin, and how do you make an instance of your own class callable like a function?
basics
~10 sDeclaring 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.
What is operator overloading in Kotlin, and how do you make the `+` symbol work on your own class?
basics
~20 sOperator 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).
How does the standard-library `to` function work, and how do `until`, `downTo`, and `step` use infix to make range code read naturally?
basics
~20 sto 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.
How do indexing (`a[i]`) and call (`a()`) syntax map to functions via operator overloading? Show `get`, `set`, and `invoke`.
basics
~10 sa[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.