Functions & Lambdas
Define functions with parameters and return types, use default and named arguments, and write concise lambda expressions.
Functions & Lambdas is a free Android Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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!
}Single-Expression Functions
When a function body is a single expression, you can use = and skip the braces and return:
fun square(n: Int): Int = n * n
fun add(a: Int, b: Int) = a + b // type inferred
fun main() {
println(square(5)) // 25
println(add(3, 7)) // 10
}Default & Named Arguments
Kotlin functions support default parameter values — no need for overloads:
- Default:
fun greet(name: String = "World") - Named: call with
greet(name = "Alice")to skip positional order
Default & Named in Action
Default arguments keep your API clean:
fun buildProfile(
name: String,
age: Int = 0,
role: String = "User"
): String {
return "$name | age=$age | $role"
}
fun main() {
println(buildProfile("Alice"))
println(buildProfile("Bob", age = 30))
println(buildProfile("Carol", 25, "Admin"))
}What Are Lambdas?
A lambda is an anonymous function — a function without a name.
- Syntax:
{ params -> body } - Stored in a variable:
val double = { x: Int -> x * 2 } - Passed to another function as an argument
Lambdas are everywhere in Android: click listeners, list transformations, coroutines.
Lambda Examples
Store and call lambdas just like regular functions:
fun main() {
val double = { x: Int -> x * 2 }
println(double(4)) // 8
val add = { a: Int, b: Int -> a + b }
println(add(3, 5)) // 8
// Lambda with no params
val sayHi = { println("Hi!") }
sayHi() // Hi!
}Higher-Order Functions
A higher-order function takes another function as a parameter or returns one.
The Kotlin standard library is full of them: filter, map, forEach.
Trailing lambda syntax: when the last argument is a lambda, move it outside the parentheses.
Trailing Lambda Syntax
Pass a lambda as the last argument outside the parentheses:
fun applyTwice(n: Int, operation: (Int) -> Int): Int {
return operation(operation(n))
}
fun main() {
val result = applyTwice(3) { x -> x * 2 }
println(result) // 12 (3 * 2 = 6, 6 * 2 = 12)
// 'it' is the implicit single parameter
val doubled = applyTwice(5) { it + it }
println(doubled) // 20
}Quick Check
Look at this code. What will it print?
val triple = { x: Int -> x * 3 }
println(triple(4))
Recap: Functions & Lambdas
You've learned:
fundefines a function with params and return type- Single-expression functions use
=shorthand - Default and named arguments replace overloads
- Lambdas are anonymous functions:
{ params -> body } - Higher-order functions accept functions as parameters
Next: controlling program flow with conditions and loops.
Frequently asked questions
Is the “Functions & Lambdas” lesson free?
Yes — the full text of “Functions & Lambdas” is free to read here on the web, and the Android Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.
What will I learn in “Functions & Lambdas”?
Define functions with parameters and return types, use default and named arguments, and write concise lambda expressions. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Android Academy?
No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Functions & Lambdas” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Android Academy lesson?
Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Variables & Data Types
- Functions & Lambdas
- Control Flow