From 9021a74e1d758da7d2040d78d06394a3db1121d0 Mon Sep 17 00:00:00 2001 From: jD91mZM2 <me@krake.one> Date: Fri, 15 Jun 2018 16:25:05 +0200 Subject: [PATCH] Manual: Mention behavior of scopes and functions --- manual/src/ch04-05-scopes.md | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/manual/src/ch04-05-scopes.md b/manual/src/ch04-05-scopes.md index ed452195..43e31158 100644 --- a/manual/src/ch04-05-scopes.md +++ b/manual/src/ch04-05-scopes.md @@ -23,6 +23,27 @@ if test 1 == 1 # end of scope, y is deleted since it's owned by it end -echo "$x" # prints 2 -echo "$y" # prints nothing, y is deleted already +echo $x # prints 2 +echo $y # prints nothing, y is deleted already +``` + +## Functions + +Functions have the scope they were defined in. +This ensures they don't use any unintended local variables that only work in some cases. +Once again, this matches the behavior of most other languages, apart from perhaps LOLCODE. + +```ion +let x = 5 # defines x + +fn print_vars + echo $x # prints 2 because it was updated before the function was called + echo $y # prints nothing, y is owned by another scope +end + +if test 1 == 1 + let x = 2 # updates existing x + let y = 3 # defines y + print_vars +end ``` -- GitLab