Merge pull request #241 from Zokrates/variable-scope-doc
Document variable scope
This commit is contained in:
commit
d71f133e16
7 changed files with 49 additions and 2 deletions
0
shadow.code
Normal file
0
shadow.code
Normal 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}}
|
||||
```
|
7
zokrates_cli/examples/book/for_scope.code
Normal file
7
zokrates_cli/examples/book/for_scope.code
Normal 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
|
7
zokrates_cli/examples/book/function_scope.code
Normal file
7
zokrates_cli/examples/book/function_scope.code
Normal file
|
@ -0,0 +1,7 @@
|
|||
def foo() -> (field):
|
||||
// return myGlobal <- not allowed
|
||||
return 42
|
||||
|
||||
def main() -> (field):
|
||||
field myGlobal = 42
|
||||
return foo()
|
7
zokrates_cli/examples/book/no_shadowing.code
Normal file
7
zokrates_cli/examples/book/no_shadowing.code
Normal 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
|
4
zokrates_cli/examples/error/out_of_for_scope.code
Normal file
4
zokrates_cli/examples/error/out_of_for_scope.code
Normal file
|
@ -0,0 +1,4 @@
|
|||
def main() -> (field):
|
||||
for field i in 0..5 do
|
||||
endfor
|
||||
return i
|
|
@ -2,6 +2,6 @@ def foo() -> (field):
|
|||
return 1
|
||||
|
||||
def main() -> (field):
|
||||
bool a
|
||||
field a = 2
|
||||
field a = foo()
|
||||
return 1
|
Loading…
Reference in a new issue