Fix call-by-value section
This commit is contained in:
parent
3cc1d691da
commit
390a738f8e
@ -220,10 +220,33 @@ Even further, one cannot assign to a variable of array type.
|
|||||||
|
|
||||||
#### Call by Value / Call by Reference
|
#### Call by Value / Call by Reference
|
||||||
|
|
||||||
`bool`, `int`, and `float` are passed directly (by value).
|
`bool`, `int`, and `float` are passed by value.
|
||||||
Strings and arrays are passed via pointers internally (by reference).
|
For arrays and strings a pointer is passed by value (similar to C).
|
||||||
|
|
||||||
Modifications made to an array inside a function are visible outside of that function.
|
Modifications made to an array inside a function are visible outside the function.
|
||||||
|
|
||||||
|
void foo(int[5] arr) {
|
||||||
|
arr[2] = 42;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
int[5] arr;
|
||||||
|
foo(arr);
|
||||||
|
print_int(arr[2]); // outputs 42
|
||||||
|
}
|
||||||
|
|
||||||
|
While strings can be re-assigned (in contrast to arrays), this is not visible outside the function call.
|
||||||
|
|
||||||
|
void foo(string s) {
|
||||||
|
s = "foo";
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
string s;
|
||||||
|
s = "bar";
|
||||||
|
foo(s);
|
||||||
|
print(s); // outputs bar
|
||||||
|
}
|
||||||
|
|
||||||
#### Type Conversion
|
#### Type Conversion
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user