Scope modifiers: Difference between revisions

Added Bracmat example
(→‎{{header|bc}}: Rephrased second paragraph)
(Added Bracmat example)
Line 214:
Global scope (before call): c = 1
Global scope (before call): d = 2</pre>
 
=={{header|Bracmat}}==
Undeclared variables have always global scope and declared variables have always dynamic scope. Also the function argument (always called "arg" and never explicitly declared) has always dynamic scope. The Bracmat program contained in file "lex.bra" (see Bracmat on GitHub) analyses another Bracmat program to find the places where variables are not in lexical scope. Following the suggestions to declare such variables (and to remove declared, but unused variables) will improve the readability of the analysed code.
 
<lang bracmat> 67:?x {x has global scope}
& 77:?y { y has global scope }
& ( double
=
. !y+!y { y refers to the variable declared in myFunc, which
shadows the global variable with the same name }
)
& ( myFunc
= y,z { y and z have dynamic scope. z is never used. }
. !arg:?y { arg is dynamically scoped }
& double$
& !x+!y
)</lang>
 
"Variables" in lambda expressions have lexical scope. But can of course not be varied.
 
<lang bracmat>/(x./(y.x$+y$)) { x and y have lexical scope }</lang>
 
=={{header|C}}==
483

edits