1
0
Fork 0
mirror of synced 2025-09-23 12:18:44 +00:00

Merge pull request #241 from Zokrates/variable-scope-doc

Document variable scope
This commit is contained in:
JacobEberhardt 2019-01-29 11:37:25 +01:00 committed by GitHub
commit d71f133e16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 2 deletions

0
shadow.code Normal file
View file

View file

@ -1,4 +1,26 @@
## Variables
Variables can have any name which does not start with a number. Underscores are not allowed in variable names.
Variables are mutable, and always passed by values to functions.
Variables are mutable, and always passed by value to functions.
### Shadowing
Shadowing is not allowed.
```zokrates
{{#include ../../../zokrates_cli/examples/book/no_shadowing.code}}
```
### Scope
#### Function
Functions have their own scope
```zokrates
{{#include ../../../zokrates_cli/examples/book/function_scope.code}}
```
#### For-loop
For-loops have their own scope
```zokrates
{{#include ../../../zokrates_cli/examples/book/for_scope.code}}
```

View file

@ -0,0 +1,7 @@
def main() -> (field):
field a = 0
for field i in 0..5 do
a = a + i
endfor
// return i <- not allowed
return a

View file

@ -0,0 +1,7 @@
def foo() -> (field):
// return myGlobal <- not allowed
return 42
def main() -> (field):
field myGlobal = 42
return foo()

View file

@ -0,0 +1,7 @@
def main() -> (field):
field a = 2
// field a = 3 <- not allowed
for field i in 0..5 do
// field a = 7 <- not allowed
endfor
return a

View file

@ -0,0 +1,4 @@
def main() -> (field):
for field i in 0..5 do
endfor
return i

View file

@ -2,6 +2,6 @@ def foo() -> (field):
return 1
def main() -> (field):
bool a
field a = 2
field a = foo()
return 1