Skip to content

Commit 73a4b2c

Browse files
Update README.md
An example to show default values for parameters
1 parent e7ecf3e commit 73a4b2c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,30 @@ fun doSomething() {
445445
}
446446
```
447447

448+
### Default values for method parameters
449+
> Java
450+
451+
```java
452+
double calculateCost(int quantity, double pricePerItem) {
453+
return pricePerItem * quantity;
454+
}
455+
456+
double calculateCost(int quantity) {
457+
// default price is 20.5
458+
return 20.5 * quantity;
459+
}
460+
```
461+
462+
> Kotlin
463+
464+
```kotlin
465+
fun calculateCost(quantity: Int, pricePerItem: Double = 20.5) = quantity * pricePerItem
466+
467+
calculateCost(10, 25.0) // 250
468+
calculateCost(10) // 205
469+
470+
```
471+
448472
---
449473
## Variable number of arguments
450474
> Java

0 commit comments

Comments
 (0)