thp-web/md/learn/functions/parameters.md

72 lines
1.2 KiB
Markdown
Raw Normal View History

2023-09-15 03:15:35 +00:00
# Function parameters
## Immutable reference
```thp
fun add_25(Array[Int] numbers) {
numbers.push(25) // Error: `numbers` is immutable
}
```
When using a regular type as a parameter, only it's immutable
properties can be used inside the function
```thp
fun count(Array[Int] numbers) -> Int {
val items_count = numbers.size() // Ok, `size` is pure
items_count
}
```
## Mutable reference
```thp
fun add_25(&Array[Int] numbers) {
numbers.push(25) // Ok, will also mutate the original array
}
```
Placing a `&` before the type makes the parameter a mutable
reference. Mutable methods can be used, and the original
data **will** be mutated.
The callee *must* also use `&`.
```thp
val numbers = Array(1, 2, 3, 4)
add_25(&numbers) // Pass `numbers` as reference.
print(numbers(4)) // `Some(25)`
```
## Clone
```thp
fun add_25(clone Array[Int] numbers) {
numbers.push(25) // Ok, the original array is unaffected
}
```
Using the `clone` keyword before the type creates a mutable copy
of the parameter. The original data will **not** be mutated.
```thp
val numbers = Array(1, 2, 3, 4)
add_25(&numbers) // Pass `numbers` as reference.
print(numbers(4)) // None
```