@@ -55,7 +55,7 @@ you can move parameters within the declaration without worrying about which is g
5555Kotlin functions can receive other functions as parameters — and be passed as arguments.
5656For details, see [ ] ( lambdas.md ) .
5757
58- ### Parameters with default values (optional parameters)
58+ ### Parameters with default values (optional parameters) {id="parameters-with-default-values"}
5959
6060Function parameters can have default values, which are used when you skip the corresponding argument.
6161This reduces the number of necessary overloads.
@@ -71,8 +71,8 @@ fun read(
7171) { /* ...*/ }
7272```
7373
74- If a parameter with default value precedes a parameter with no default value, the default value can only be used by calling
75- the function with [ named arguments] ( #named-arguments ) :
74+ When you declare a parameter with a default value before a parameter without a default value,
75+ you can only use the default value by [ naming arguments] ( #named-arguments ) :
7676
7777``` kotlin
7878fun foo (
@@ -84,6 +84,18 @@ foo(bar = 1) // Uses the default value foo = 0
8484foo(1 ) // Error: No value passed for parameter 'bar'
8585```
8686
87+ [ Trailing lambdas] ( lambdas.md#passing-trailing-lambdas ) are an exception to this rule,
88+ since the last parameter must correspond to the passed function:
89+
90+ ``` kotlin
91+ fun foo (
92+ foo : Int = 0,
93+ bar : () -> Unit ,
94+ ) { bar() }
95+
96+ foo() { println (" bar" ) } // Uses the default value for 'foo' and prints "bar"
97+ ```
98+
8799[ Overriding methods] ( inheritance.md#overriding-methods ) always use the base method's default parameter values.
88100When overriding a method that has default parameter values, the default parameter values must be omitted from the signature:
89101
@@ -213,16 +225,41 @@ foo(strings = arrayOf("a", "b", "c"))
213225>
214226 {style="note"}
215227
216- ### Unit-returning functions
228+ ### Explicit return types
229+
230+ Functions with a block body must always specify return types explicitly, unless it's intended for them to return ` Unit ` ,
231+ [ in which case specifying the return type is optional] ( #unit-returning-functions ) .
217232
218- If a function does not return a useful value, its return type is ` Unit ` (corresponds to the ` void ` type in Java).
233+ Kotlin does not infer return types for functions with block bodies because such functions may have complex control flow
234+ in the body, and the return type will be non-obvious to the reader (and sometimes even for the compiler).
235+ But the return type is inferred, unless specified, for [ single-expression functions] ( #single-expression-functions ) .
236+
237+ ### Single-expression functions
238+
239+ When the function body consists of a single expression, the curly braces can be omitted and the body specified after an ` = ` symbol:
240+
241+ ``` kotlin
242+ fun double (x : Int ): Int = x * 2
243+ ```
244+
245+ Explicitly declaring the return type is [ optional] ( #explicit-return-types ) when this can be inferred by the compiler:
246+
247+ ``` kotlin
248+ fun double (x : Int ) = x * 2
249+ ```
250+
251+ ### Unit-returning functions
219252
253+ If a function has a block body and does not return a useful value, its return type is assumed to be ` Unit ` (corresponds to the ` void ` type in Java).
220254` Unit ` is a type with only one value - ` Unit ` .
221- You don't have to declare this return type, or return ` Unit ` explicitly.
255+
256+ You don't have to specify ` Unit ` as a return type, except for functional type parameters,
257+ and you never have to return ` Unit ` explicitly.
258+
222259Therefore, this verbose declaration:
223260
224261``` kotlin
225- fun printHello (name : String? ): Unit {
262+ fun printHello (name : String? , aux : () -> Unit ): Unit {
226263 if (name != null )
227264 println (" Hello $name " )
228265 else
@@ -231,20 +268,18 @@ fun printHello(name: String?): Unit {
231268}
232269```
233270
234- is equivalent to:
271+ can be shortened to:
235272
236273``` kotlin
237- fun printHello (name : String? ) { /* ...*/ }
274+ // The declaration of 'aux' still needs an explicit return type
275+ fun printHello (name : String? , aux : () -> Unit ) {
276+ if (name != null )
277+ println (" Hello $name " )
278+ else
279+ println (" Hi there!" )
280+ }
238281```
239282
240- ### Explicit return types
241-
242- Functions with block body must always specify return types explicitly, unless it's intended for them to return ` Unit ` ,
243- [ in which case specifying the return type is optional] ( #unit-returning-functions ) .
244-
245- Kotlin does not infer return types for functions with block bodies because such functions may have complex control flow
246- in the body, and the return type will be non-obvious to the reader (and sometimes even for the compiler).
247-
248283### Variable number of arguments (varargs)
249284
250285You can mark a parameter of a function (usually the last one) with the ` vararg ` modifier:
@@ -287,20 +322,6 @@ val a = intArrayOf(1, 2, 3) // IntArray is a primitive type array
287322val list = asList(- 1 , 0 , * a.toTypedArray(), 4 )
288323```
289324
290- ### Single-expression functions
291-
292- When the function body consists of a single expression, the curly braces can be omitted and the body specified after an ` = ` symbol:
293-
294- ``` kotlin
295- fun double (x : Int ): Int = x * 2
296- ```
297-
298- Explicitly declaring the return type is [ optional] ( #explicit-return-types ) when this can be inferred by the compiler:
299-
300- ``` kotlin
301- fun double (x : Int ) = x * 2
302- ```
303-
304325### Infix notation
305326
306327Functions marked with the ` infix ` keyword can also be called using the infix notation (omitting the dot and the parentheses
@@ -309,7 +330,7 @@ for the call). Infix functions must meet the following requirements:
309330* They must be member functions or [ extension functions] ( extensions.md ) .
310331* They must have a single parameter.
311332* The parameter must not [ accept variable number of arguments] ( #variable-number-of-arguments-varargs ) and must have
312- no [ default value] ( #parameters-with-default-values-optional-parameters ) .
333+ no [ default value] ( #parameters-with-default-values ) .
313334
314335``` kotlin
315336infix fun Int.shl (x : Int ): Int { /* ...*/ }
0 commit comments