Discover how tiny pieces of reusable code can make your app smarter and your life easier!
Why Functions and lambdas in Android Kotlin? - Purpose & Use Cases
Imagine you want to repeat a task many times in your app, like showing a message or calculating a value. Without functions, you would have to write the same code again and again everywhere.
Writing the same code multiple times is slow and tiring. It's easy to make mistakes or forget to update all copies when you want to change something. This makes your app buggy and hard to fix.
Functions let you write a piece of code once and use it many times by calling its name. Lambdas are small, quick functions you can write right where you need them, making your code neat and easy to understand.
println("Hello") println("Hello") println("Hello")
fun greet() { println("Hello") }
greet()
greet()
greet()With functions and lambdas, you can build apps that are easier to read, fix, and grow without repeating yourself.
When you tap a button in an app, a lambda can quickly handle what happens next, like showing a message or starting a new screen, all without cluttering your code.
Functions save you from repeating code.
Lambdas let you write quick, small actions inline.
Both make your app cleaner and easier to maintain.
Practice
val sum = { a: Int, b: Int -> a + b }Solution
Step 1: Understand lambda syntax
The code{ a: Int, b: Int -> a + b }defines a function without a name.Step 2: Identify lambda characteristics
This function is stored in the variablesum, showing it is unnamed but reusable.Final Answer:
An unnamed function that can be stored in a variable -> Option BQuick Check:
Lambda = unnamed function stored in variable [OK]
- Thinking lambdas must have names
- Confusing lambdas with classes
- Assuming lambdas are loops
Solution
Step 1: Review lambda syntax for no parameters
Lambdas with no parameters use{ expression }syntax.Step 2: Check each option
val greet = { "Hello" } correctly uses{ "Hello" }. val greet = { -> "Hello" missing closing brace, B is function type syntax, D is a named function.Final Answer:
val greet = { "Hello" } -> Option DQuick Check:
No-parameter lambda = { value } [OK]
- Missing closing brace in lambda
- Confusing lambda with function type syntax
- Using fun keyword inside lambda
val multiply = { x: Int, y: Int -> x * y }
println(multiply(3, 4))Solution
Step 1: Understand the lambda function
The lambda{ x: Int, y: Int -> x * y }multiplies two integers.Step 2: Calculate the result of multiply(3, 4)
3 * 4 equals 12, so the printed output is 12.Final Answer:
12 -> Option CQuick Check:
3 * 4 = 12 [OK]
- Adding instead of multiplying
- Concatenating numbers as strings
- Expecting syntax error
val greet = { name: String ->
println("Hello, " + name)
}Solution
Step 1: Check lambda syntax
The lambda takes a String parameter and calls println, which is allowed.Step 2: Confirm return type flexibility
Lambdas can return Unit (like void), so no return type declaration is needed.Final Answer:
No error, this is valid syntax -> Option AQuick Check:
Lambda with println is valid [OK]
- Expecting return type declaration
- Thinking println is disallowed
- Confusing Unit with error
val numbers = listOf(1, 2, 3, 4, 5, 6) val evens = numbers.filter( /* fill here */ ) println(evens)
Solution
Step 1: Understand filtering with lambda
The filter lambda should return true for even numbers using modulo operator.Step 2: Evaluate each option
numbers.filter { it % 2 == 0 } correctly usesit % 2 == 0. numbers.filter { x -> x / 2 == 0 } uses division instead of modulo, C uses assignment (=) instead of comparison (==), A uses bitwise AND incorrectly.Final Answer:
numbers.filter { it % 2 == 0 } -> Option AQuick Check:
Filter even numbers with modulo == 0 [OK]
- Using = instead of == for comparison
- Using division instead of modulo
- Misusing bitwise operators
Ready to prove it?
Pass the test and this pattern moves unknown
