Functions & Lambdas
Define functions with parameters and return types, use default and named arguments, and write concise lambda expressions.
Why Functions?
Functions let you write a block of code once and reuse it anywhere.
- Reduce duplication
- Make code easier to read and test
- Break big problems into smaller pieces
In Kotlin, functions are first-class — you can store them in variables and pass them around.
Basic Function Syntax
A Kotlin function uses the fun keyword:
fun greet(name: String): String {
return "Hello, $name!"
}
fun main() {
val msg = greet("Alice")
println(msg) // Hello, Alice!
println(greet("Bob")) // Hello, Bob!
}All lessons in this course
- Variables & Data Types
- Functions & Lambdas
- Control Flow