@@ -86,17 +86,17 @@ When you declare a parameter with a default value before a parameter without a d
8686you can only use the default value by [ naming arguments] ( #named-arguments ) :
8787
8888``` kotlin
89- fun foo (
90- foo : Int = 0,
91- bar : Int ,
89+ fun greeting (
90+ userId : Int = 0,
91+ message : String ,
9292) { /* ...*/ }
9393
9494fun main () {
95- // Uses the default value foo = 0
96- foo(bar = 1 )
95+ // Uses 0 as the default value for 'userId'
96+ greeting(message = " Hello! " )
9797
98- // Error: No value passed for parameter 'bar '
99- foo( 1 )
98+ // Error: No value passed for parameter 'userId '
99+ greeting( " Hello! " )
100100}
101101```
102102{kotlin-runnable="true" kotlin-min-compiler-version="1.3" validate="false" id="default-before-ordinary"}
@@ -107,16 +107,16 @@ since the last parameter must correspond to the passed function:
107107``` kotlin
108108fun main () {
109109// sampleStart
110- fun foo (
111- foo : Int = 0,
112- bar : () -> Unit ,
110+ fun greeting (
111+ userId : Int = 0,
112+ message : () -> Unit ,
113113)
114- { println (foo )
115- bar () }
114+ { println (userId )
115+ message () }
116116
117117
118- // Prints the default value 0 for 'foo ', then prints "bar"
119- foo () { println (" bar " ) }
118+ // Prints the default value 0 for 'userId ', then prints "bar"
119+ greeting () { println (" Hello! " ) }
120120// sampleEnd
121121}
122122```
@@ -126,15 +126,15 @@ foo() { println ("bar") }
126126When overriding a method that has default parameter values, you must omit the default parameter values from the signature:
127127
128128``` kotlin
129- open class A {
130- open fun foo ( i : Int = 10, j : Int = 0 ) { /* ...*/ }
129+ open class Shape {
130+ open fun draw ( width : Int = 10, height : Int = 5 ) { /* ...*/ }
131131}
132132
133- class B : A () {
133+ class Rectangle : Shape () {
134134 // It's not allowed to specify default values here
135- // but this function also uses 10 for 'i ' and 0 for 'j '
135+ // but this function also uses 10 for 'width ' and 5 for 'height '
136136 // by default.
137- override fun foo ( i : Int , j : Int ) { /* ...*/ }
137+ override fun draw ( width : Int , height : Int ) { /* ...*/ }
138138}
139139```
140140
@@ -182,22 +182,22 @@ you can pass the corresponding [lambda](lambdas.md#lambda-expression-syntax) arg
182182``` kotlin
183183fun main () {
184184// sampleStart
185- fun foo (
186- foo : Int = 0,
187- bar : Int = 1,
188- qux : () -> Unit ,
189- ) { println (foo )
190- println (bar )
191- qux () }
185+ fun log (
186+ level : Int = 0,
187+ code : Int = 1,
188+ action : () -> Unit ,
189+ ) { println (level )
190+ println (code )
191+ action () }
192192
193- // Passes foo = 1 and ses the default value bar = 1
194- foo (1 ) { println (" hello " ) }
193+ // Passes 1 for 'level' and uses the default value 1 for 'code'
194+ log (1 ) { println (" Connection established " ) }
195195
196- // Uses both default values, foo = 0 and bar = 1
197- foo(qux = { println (" hello " ) })
196+ // Uses both default values, 0 for 'level' and 1 for 'code'
197+ log(action = { println (" Connection established " ) })
198198
199199// Equivalent to the previous call, uses both default values
200- foo { println (" hello " ) }
200+ log { println (" Connection established " ) }
201201// sampleEnd
202202}
203203```
@@ -206,7 +206,7 @@ foo { println("hello") }
206206### Named arguments
207207
208208You can name one or more of a function's arguments when calling it. This can be helpful when a function call has many
209- argument and it's difficult to associate a value with an argument, especially if it's a boolean or ` null ` value.
209+ argument, and it's difficult to associate a value with an argument, especially if it's a boolean or ` null ` value.
210210
211211When you use named arguments in a function call, you can freely change the order that they are listed in.
212212
@@ -250,9 +250,9 @@ reformat("This is a short String!", upperCaseFirstLetter = false, wordSeparator
250250You can pass a [ variable number of arguments] ( #variable-number-of-arguments-varargs ) (` vararg ` ) naming the correspoding array:
251251
252252``` kotlin
253- fun foo (vararg strings : String ) { /* ...*/ }
253+ fun mergeStrings (vararg strings : String ) { /* ...*/ }
254254
255- foo (strings = arrayOf(" a" , " b" , " c" ))
255+ mergeStrings (strings = arrayOf(" a" , " b" , " c" ))
256256```
257257
258258<!-- Rationale for named arguments interaction with varargs is here https://youtrack.jetbrains.com/issue/KT-52505#focus=Comments-27-6147916.0-0 -->
@@ -297,7 +297,7 @@ and you never have to return `Unit` explicitly.
297297Therefore, this verbose declaration:
298298
299299``` kotlin
300- fun printHello (name : String? , aux : () -> Unit ): Unit {
300+ fun printHello (name : String? , action : () -> Unit ): Unit {
301301 if (name != null )
302302 println (" Hello $name " )
303303 else
@@ -309,9 +309,9 @@ fun printHello(name: String?, aux: () -> Unit): Unit {
309309can be shortened to:
310310
311311``` kotlin
312- // The declaration of the functional type parameter ('aux ') still
312+ // The declaration of the functional type parameter ('action ') still
313313// needs an explicit return type
314- fun printHello (name : String? , aux : () -> Unit ) {
314+ fun printHello (name : String? , action : () -> Unit ) {
315315 if (name != null )
316316 println (" Hello $name " )
317317 else
0 commit comments