@@ -32,6 +32,14 @@ Parameters are separated using commas, and each parameter must be explicitly typ
3232fun powerOf (number : Int , exponent : Int ): Int { /* ...*/ }
3333```
3434
35+ Inside the body of a function, received parameters are read-only (implicitly ` val ` ):
36+
37+ ``` kotlin
38+ fun powerOf (number : Int , exponent : Int ): Int {
39+ number = 2 // Error: 'val' cannot be reassigned.
40+ }
41+ ```
42+
3543You can use a [ trailing comma] ( coding-conventions.md#trailing-commas ) when you declare function parameters:
3644
3745``` kotlin
@@ -44,6 +52,9 @@ fun powerOf(
4452This helps with refactorings and code maintenance:
4553you can move parameters within the declaration without worrying about which is going to be the last one.
4654
55+ Kotlin functions can receive other functions as parameters — and be passed as arguments.
56+ For details, see [ ] ( lambdas.md ) .
57+
4758### Parameters with default values (optional parameters)
4859
4960Function parameters can have default values, which are used when you skip the corresponding argument.
@@ -64,19 +75,14 @@ If a parameter with default value precedes a parameter with no default value, th
6475the function with [ named arguments] ( #named-arguments ) :
6576
6677``` kotlin
67- fun main () {
68- // sampleStart
6978fun foo (
7079 foo : Int = 0,
7180 bar : Int ,
7281) { /* ...*/ }
7382
7483foo(bar = 1 ) // Uses the default value foo = 0
7584foo(1 ) // Error: No value passed for parameter 'bar'
76- // sampleEnd
77- }
7885```
79- {kotlin-runnable="true" kotlin-min-compiler-version="1.3"}
8086
8187[ Overriding methods] ( inheritance.md#overriding-methods ) always use the base method's default parameter values.
8288When overriding a method that has default parameter values, the default parameter values must be omitted from the signature:
@@ -96,7 +102,7 @@ class B : A() {
96102
97103#### Non-constant expressions as default values
98104
99- You can assign to a parameter a default value that is not constant an expression , as in a function call, or a calculation that uses
105+ You can assign to a parameter a default value that is not constant, as in a function call, or a calculation that uses
100106values of other arguments, like the ` len ` parameter in the example above:
101107
102108``` kotlin
@@ -107,20 +113,23 @@ fun read(
107113) { /* ...*/ }
108114```
109115
110- Parameters referring to other parameters' values must be declared later in the order (in this example, ` len ` must be declared after ` b ` ).
116+ Parameters referring to other parameters' values must be declared later in the order
117+ (in this example, ` len ` must be declared after ` b ` ).
111118
112- In general default value of a parameter can be any expression — but such expressions are only calculated when
113- the function is called ** without** the parameter and a default value needs to be assigned.
114- For example, this function prints out a line when it is called without the ` print ` parameter:
119+ In general default value of a parameter can be any expression.
120+ But such expressions are only calculated when the function is called ** without** the corresponding parameter
121+ and a default value needs to be assigned.
122+ For example, this function prints out a line only when it is called without the ` print ` parameter:
115123
116124``` kotlin
117125fun read (
118126 b : Int ,
119127 print : Unit? = println("No argument passed for 'print'.")
120- ) { /* ... */ }
128+ ) { println (b) }
121129
122130fun main () {
123- read (1 )
131+ read (1 ) // BothFirst "No argument passed for 'print'.", then "1" is printed
132+ read (1 , null ) // Only the "1" is printed
124133}
125134```
126135
@@ -343,7 +352,8 @@ class MyStringCollection {
343352
344353## Function scope
345354
346- Kotlin functions can be declared at the top level in a file, meaning you do not need to create a class to hold a function.
355+ Kotlin functions can be declared at the top level in a file, meaning you do not need to create a class to hold a function
356+ (unlike Java, for example).
347357Functions can also be declared locally as _ member functions_ and _ extension functions_ .
348358
349359### Local functions
@@ -438,8 +448,6 @@ private fun findFixPoint(): Double {
438448
439449To be eligible for the ` tailrec ` modifier, a function must call itself as the last operation it performs. You cannot use
440450tail recursion when there is more code after the recursive call, within ` try ` /` catch ` /` finally ` blocks, or on open functions.
441- Currently, tail recursion is supported by Kotlin for the JVM and Kotlin/Native.
442- TODO what else is there but JVM and Native?
443451
444452** See also** :
445453* [ Inline functions] ( inline-functions.md )
0 commit comments