← All courses

Functions, Lambdas and Collections

🗓 May 31, 2026 ⏱ 1 min read

Functions

fun add(a: Int, b: Int): Int = a + b      // single-expression
fun greet(name: String = "friend") = "Hi $name"  // default value

Lambdas

A lambda is a function you pass around like a value:

val square = { x: Int -> x * x }
println(square(5))   // 25

Collections are a superpower

val nums = listOf(1, 2, 3, 4, 5)

val evens = nums.filter { it % 2 == 0 }   // [2, 4]
val doubled = nums.map { it * 2 }         // [2, 4, 6, 8, 10]
val total = nums.sum()                    // 15
Tip: filter, map, forEach and sumOf let you transform data clearly without manual loops.