You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/docs/reference/functions.md
+28-1Lines changed: 28 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -91,6 +91,23 @@ class B : A() {
91
91
}
92
92
```
93
93
94
+
If a default parameter precedes a parameter with no default value, the default value can be used only by calling the function with [named arguments](#named-arguments):
95
+
96
+
```kotlin
97
+
funfoo(bar:Int = 0, baz:Int) { /* ... */ }
98
+
99
+
foo(baz =1) // The default value bar = 0 is used
100
+
```
101
+
102
+
But if a last argument [lambda](lambdas.html#lambda-expression-syntax) is passed to a function call outside the parentheses, passing no values for the default parameters is allowed:
foo(1) { println("hello") } // Uses the default value baz = 1
108
+
foo { println("hello") } // Uses both defeault values bar = 0 and baz = 1
109
+
```
110
+
94
111
### Named Arguments
95
112
96
113
Function parameters can be named when calling functions. This is very convenient when a function has a high number of parameters or default ones.
@@ -136,10 +153,20 @@ and if we do not need all arguments
136
153
reformat(str, wordSeparator ='_')
137
154
```
138
155
156
+
When a function is called with both positional and named arguments, all the positional arguments should be placed before the first named one. For example, the call `f(1, y = 2)` is allowed, but `f(x = 1, 2)` is not.
157
+
158
+
[Variable number of arguments (*vararg*{: .keyword })](#variable-number-of-arguments-varargs) can be passed in the named form by using the **spread** operator:
159
+
160
+
```kotlin
161
+
funfoo(varargstrings:String) { /* ... */ }
162
+
163
+
foo(strings =*arrayOf("a", "b", "c"))
164
+
foo(strings ="a") // Not required for a single value
165
+
```
166
+
139
167
Note that the named argument syntax cannot be used when calling Java functions, because Java bytecode does not
140
168
always preserve names of function parameters.
141
169
142
-
143
170
### Unit-returning functions
144
171
145
172
If a function does not return any useful value, its return type is `Unit`. `Unit` is a type with only one value - `Unit`. This
0 commit comments