Functions Workbook


Practice problems for In Kotlin, Functions Don’t Need a Class. Each takes a minute or two. Write your own answer first, then click Show answer to check — nothing here is a trick question, just direct practice of the syntax from the lesson.

Declaring and single-expression functions

1. Square a number

Write a single-expression function square that takes an Int and returns it multiplied by itself.

Show answer Hide answer
fun square(x: Int) = x * x

2. The larger of two

Write max(a: Int, b: Int) that returns the larger of the two, using if as an expression.

Show answer Hide answer
fun max(a: Int, b: Int) = if (a > b) a else b

3. Tighten a block body

Rewrite this as a single-expression function:

fun double(n: Int): Int {
    return n * 2
}
Show answer Hide answer
fun double(n: Int) = n * 2

4. A function that returns nothing

Write greet(name: String) that prints Hi, <name> and returns no meaningful value. What is its return type?

Show answer Hide answer
fun greet(name: String) {
    println("Hi, $name")
}

The return type is Unit — the default when you write no return type, and Kotlin’s stand-in for void.

5. Build a full name

Write fullName(first: String, last: String) that returns the two joined by a space, using a string template.

Show answer Hide answer
fun fullName(first: String, last: String) = "$first $last"

Default and named arguments

6. A default port

Write connect(host: String, port: Int = 8080) that returns "host:port". Calling connect("localhost") should give "localhost:8080".

Show answer Hide answer
fun connect(host: String, port: Int = 8080) = "$host:$port"

7. Skip to the argument you want

Given fun connect(host: String, port: Int = 443, timeoutMs: Int = 5000), write a call that keeps the default port but sets timeoutMs to 10000.

Show answer Hide answer
connect("example.com", timeoutMs = 10000)

A named argument lets you skip over an earlier default.

8. Replace two overloads with one

In Java you’d write two methods: String label(String text) and String label(String text, boolean bold). Write a single Kotlin function label where bold defaults to false; return the text wrapped in <b>…</b> when bold is true, otherwise the text unchanged.

Show answer Hide answer
fun label(text: String, bold: Boolean = false) =
    if (bold) "<b>$text</b>" else text

9. Arguments out of order

Given fun rect(width: Int, height: Int), call it with the arguments written in the opposite order, using named arguments.

Show answer Hide answer
rect(height = 20, width = 10)

Named arguments free a call from positional order entirely.

vararg

10. Sum any number of values

Write sum that accepts any number of Int arguments and returns their total.

Show answer Hide answer
fun sum(vararg n: Int) = n.sum()

11. Pass an array into a vararg

Given fun sum(vararg n: Int): Int and val xs = intArrayOf(1, 2, 3), write the call that passes the array’s elements as the individual arguments.

Show answer Hide answer
sum(*xs)

The * is the spread operator — it unpacks the array into separate arguments.

12. Join words

Write joinWords(vararg words: String) that returns all the words joined by single spaces.

Show answer Hide answer
fun joinWords(vararg words: String) = words.joinToString(" ")

Top-level and local functions

13. A utility with no class

Write gcd(a: Int, b: Int) as a top-level, single-expression recursive function (greatest common divisor). No surrounding class.

Show answer Hide answer
fun gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)

A recursive single-expression function needs its return type written out, since it can’t be inferred from a body that refers to itself.

14. A helper that stays local

Inside a function banner(text: String), define a local function line() that prints 20 dashes, then use it above and below the text.

Show answer Hide answer
fun banner(text: String) {
    fun line() = println("-".repeat(20))

    line()
    println(text)
    line()
}

line exists only inside banner — no helper leaks into the wider namespace.

15. Make a call self-documenting

The call setEnabled(true) tells a reader nothing. Given fun setEnabled(enabled: Boolean), rewrite the call so it reads itself.

Show answer Hide answer
setEnabled(enabled = true)

Done? Head back to the lesson, In Kotlin, Functions Don’t Need a Class, or move on to the next one: control flow and ranges.