Discover how one simple change can make your functions cleaner and your code happier!
Why Single-expression functions in Kotlin? - Purpose & Use Cases
Imagine you want to write a simple function that adds two numbers. You write multiple lines with curly braces and return statements every time. It feels like writing a whole paragraph just to say one thing.
This long way is slow to write and read. It clutters your code with extra words and braces. When you want to see what the function does, you have to scroll through unnecessary lines. It's easy to make mistakes or miss the main point.
Single-expression functions let you write that same function in one clean line. You just write the function name, parameters, and the expression it returns. No braces, no return keyword. It makes your code shorter, clearer, and easier to understand.
fun add(a: Int, b: Int): Int {
return a + b
}fun add(a: Int, b: Int) = a + b
It enables writing clear and concise functions that are easy to read and maintain, making your code more elegant and less error-prone.
When creating small helper functions like calculating a discount or formatting a string, single-expression functions let you write them quickly and keep your code neat.
Manual functions can be long and cluttered.
Single-expression functions simplify code to one line.
This makes code easier to read and write.
Practice
single-expression function in Kotlin?Solution
Step 1: Understand single-expression function syntax
Single-expression functions use an equals sign followed by an expression, without braces.Step 2: Identify the return behavior
They automatically return the value of that expression without needing an explicit return keyword.Final Answer:
It returns the value of the expression after the equals sign automatically. -> Option AQuick Check:
Single-expression function = auto return [OK]
- Thinking explicit return is needed
- Assuming parameters are not allowed
- Believing it must be inside a class
Solution
Step 1: Identify single-expression function syntax
It uses '=' followed by the expression without braces or explicit return.Step 2: Check each option
fun add(a: Int, b: Int) = a + b matches this syntax exactly:fun add(a: Int, b: Int) = a + b.Final Answer:
fun add(a: Int, b: Int) = a + b -> Option BQuick Check:
Equals sign and expression = correct syntax [OK]
- Using braces with return in single-expression functions
- Using '=>' instead of '='
- Omitting '=' sign
fun square(x: Int) = x * x println(square(4))
Solution
Step 1: Understand the function behavior
The functionsquarereturns the product ofxtimesx.Step 2: Calculate the output for input 4
4 * 4 = 16, sosquare(4)returns 16, which is printed.Final Answer:
16 -> Option AQuick Check:
4 squared = 16 [OK]
- Expecting the expression as a string output
- Confusing input with output
- Thinking it causes a compile error
fun greet(name: String) = {
"Hello, $name!"
}Solution
Step 1: Check single-expression function syntax
Single-expression functions use '=' followed by an expression without braces.Step 2: Identify the error
The braces after '=' create a lambda block, which is not allowed here; it should be just the expression.Final Answer:
Using braces {} after equals sign is incorrect. -> Option CQuick Check:
No braces allowed after '=' in single-expression functions [OK]
- Adding braces after '='
- Thinking return type is mandatory
- Misusing string interpolation syntax
Solution
Step 1: Understand the requirement
The function must return length if string is not empty, else zero, in a single expression.Step 2: Check each option
fun lengthOrZero(s: String) = if (s.isNotEmpty()) s.length else 0 uses a single-expression function with an if-else expression correctly. fun lengthOrZero(s: String) { if (s.isNotEmpty()) return s.length else return 0 } uses braces and explicit returns, not single-expression. fun lengthOrZero(s: String): Int = { if (s.isNotEmpty()) s.length else 0 } wrongly uses braces after '='. fun lengthOrZero(s: String) = s.length ?: 0 misuses the Elvis operator on Int.Final Answer:
fun lengthOrZero(s: String) = if (s.isNotEmpty()) s.length else 0 -> Option DQuick Check:
If-else expression after '=' = correct single-expression function [OK]
- Using braces after '='
- Trying to use return inside single-expression function
- Misusing Elvis operator on non-nullable Int
Ready to prove it?
Pass the test and this pattern moves unknown
