From 390a738f8e3ea2737265bd73e444dc7820f90da7 Mon Sep 17 00:00:00 2001 From: Alex Hirsch Date: Mon, 17 Jun 2019 08:50:10 +0200 Subject: [PATCH] Fix call-by-value section --- specification.md | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/specification.md b/specification.md index 7aabe28..a5098f7 100644 --- a/specification.md +++ b/specification.md @@ -220,10 +220,33 @@ Even further, one cannot assign to a variable of array type. #### Call by Value / Call by Reference -`bool`, `int`, and `float` are passed directly (by value). -Strings and arrays are passed via pointers internally (by reference). +`bool`, `int`, and `float` are passed by value. +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