Skip to content

'Default arguments' -> 'Default parameters' #4833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/topics/coding-conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ class Point(val x: Double, val y: Double) {
```

If you have an object with multiple overloaded constructors that don't call different superclass constructors and
can't be reduced to a single constructor with default argument values, prefer to replace the overloaded constructors with
can't be reduced to a single constructor with optional parameters, prefer to replace the overloaded constructors with
factory functions.

### Platform types
Expand Down
4 changes: 2 additions & 2 deletions docs/topics/collection-transformations.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ strings: [`joinToString()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.
`joinToString()` builds a single `String` from the collection elements based on the provided arguments.
`joinTo()` does the same but appends the result to the given [`Appendable`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/-appendable/index.html) object.

When called with the default arguments, the functions return the result similar to calling `toString()` on the collection:
a `String` of elements' string representations separated by commas with spaces.
When called with the parameters' default values, the functions return the result similar to calling `toString()` on the collection:
a `String` of elements' string representations separated by commas with spaces.

```kotlin

Expand Down
14 changes: 8 additions & 6 deletions docs/topics/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ fun powerOf(
) { /*...*/ }
```

### Default arguments
### Parameters with default values

Function parameters can have default values, which are used when you skip the corresponding argument. This reduces the number
of overloads:
Function parameters can have default values, which are used when you skip the corresponding argument.
This reduces the number of overloads:

```kotlin
fun read(
Expand All @@ -53,6 +53,8 @@ fun read(
) { /*...*/ }
```

Such parameters are sometimes referred as _optional parameters_.

A default value is set by appending `=` to the type.

Overriding methods always use the base method's default parameter values.
Expand All @@ -68,7 +70,7 @@ class B : A() {
}
```

If a default parameter precedes a parameter with no default value, the default value can only be used by calling
If a parameter with default value precedes a parameter with no default value, the default value can only be used by calling
the function with [named arguments](#named-arguments):

```kotlin
Expand All @@ -80,8 +82,8 @@ fun foo(
foo(baz = 1) // The default value bar = 0 is used
```

If the last argument after default parameters is a [lambda](lambdas.md#lambda-expression-syntax),
you can pass it either as a named argument or [outside the parentheses](lambdas.md#passing-trailing-lambdas):
If the last parameter after all optional parameters has a functional type,
then you can pass the corresponding [lambda](lambdas.md#lambda-expression-syntax) argument either as a named argument or [outside the parentheses](lambdas.md#passing-trailing-lambdas):

```kotlin
fun foo(
Expand Down
2 changes: 1 addition & 1 deletion docs/topics/js/js-interop.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class Bar : Foo() {
There are some limitations:

- When a function of an external base class is overloaded by signature, you can't override it in a derived class.
- You can't override a function with default arguments.
- You can't override a function with optional parameters.
- Non-external classes can't be extended by external classes.

### external interfaces
Expand Down
2 changes: 1 addition & 1 deletion docs/topics/jvm/comparison-to-java.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Kotlin fixes a series of issues that Java suffers from:
* [Data classes](data-classes.md)
* [Coroutines](coroutines-overview.md)
* [Top-level functions](functions.md)
* [Default arguments](functions.md#default-arguments)
* [Parameters with default values](/docs/topics/functions.md#parameters-with-default-values)
* [Named parameters](functions.md#named-arguments)
* [Infix functions](functions.md#infix-notation)
* [Expect and actual declarations](multiplatform-expect-actual.md)
Expand Down