October 2025
Intermediate to advanced
100 pages
2h 27m
English
There are two ways to introduce the body of a function in Kotlin: curly braces, which mark out a block of code that should be executed when the function is called, or an equal sign (=), which specifies a value for the function to return.
This program’s bug is caused by mixing up the two styles. The code block in makeLatte looks deceptively like an executable function body, but because it has an equal sign before it, it’s actually the function’s return value. That means the code block doesn’t run when makeLatte is called. Instead, it’s returned to the caller as a lambda function for later execution.
Let’s use a simpler example to compare the two ways of writing a function. First, we’ll add two numbers using ...
Read now
Unlock full access