TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
return expression in Kotlin is a control flow construct that immediately terminates the execution of the innermost enclosing function or anonymous function, yielding a specified value to the caller. In Kotlin’s type system, return is an expression that evaluates to the bottom type Nothing. This signifies that the expression never completes normally, allowing it to be used in contexts expecting any arbitrary type.
Syntax
Evaluation and Type Mechanics
Becausereturn evaluates to Nothing, it can be used as an operand in expressions (such as the Elvis operator) without causing type mismatches. The compiler statically determines that any code following a return expression in the same control flow branch is unreachable.
Scope and Resolution
The execution target of areturn expression depends strictly on its lexical context, the fun keyword, and the presence of labels.
Unlabeled Returns
An unlabeledreturn always terminates the nearest enclosing function declared with the fun keyword.
Non-Local Returns
When an unlabeledreturn expression is used inside a lambda expression passed to an inline higher-order function, it performs a non-local return. It bypasses the lambda’s scope and terminates the nearest enclosing fun.
Labeled Returns
To terminate a lambda expression without exiting the enclosing function, a labeledreturn is required. The label explicitly defines the execution scope to terminate, effectively acting as a local return for the lambda.
Anonymous Functions
Unlike lambdas, anonymous functions are explicitly declared using thefun keyword. Consequently, an unlabeled return inside an anonymous function resolves to the anonymous function itself, not the enclosing function.
Master Kotlin with Deep Grasping Methodology!Learn More





