Scope modifiers: Difference between revisions
Content added Content deleted
No edit summary |
No edit summary |
||
Line 699: | Line 699: | ||
extends(public::minimal)). |
extends(public::minimal)). |
||
</lang> |
</lang> |
||
=={{header|M2000 Interpreter}}== |
|||
We can use Global, Local to shadow any sane variable and give scope to new one. |
|||
We can use Static for simple variables, so we can find them in the next call. |
|||
By default every new variable/module/function/group is local if defined in a module or a function |
|||
In command line, in M2000 console every variable/module/function/group is global |
|||
Local variables are prefered by interpreter at reading/writing |
|||
We have to use <= to assign new values to global variables, or to member of groups inside a module inside a group. See ResetValues in Group Alfa. |
|||
<lang M2000 Interpreter> |
|||
Module Checkit { |
|||
M=1000 |
|||
Function Global xz { |
|||
=9999 |
|||
} |
|||
Module TopModule { |
|||
\\ clear vars and static vars |
|||
Clear |
|||
M=500 |
|||
Function Global xz { |
|||
=10000 |
|||
} |
|||
Module Kappa { |
|||
Static N=1 |
|||
Global M=1234 |
|||
x=1 |
|||
z=1 |
|||
k=1 |
|||
Group Alfa { |
|||
Private: |
|||
x=10, z=20, m=100 |
|||
Function xz { |
|||
=.x*.z+M |
|||
}7 |
|||
Public: |
|||
k=50 |
|||
Module AddOne { |
|||
.x++ |
|||
.z++ |
|||
.k++ |
|||
Print .xz(), .m=100 |
|||
} |
|||
Module ResetValues { |
|||
\\ use <= to change members, else using = we define local variables |
|||
.x<=10 |
|||
.z<=20 |
|||
} |
|||
} |
|||
' print 1465 |
|||
Alfa.AddOne |
|||
Print x=1, z=1, k=1, xz()=10000 |
|||
Print N ' 1 first time, 2 second time |
|||
N++ |
|||
Push Alfa |
|||
} |
|||
Kappa |
|||
Drop ' drop one alfa |
|||
Kappa |
|||
Print M=500 |
|||
' leave one alfa in stack of values |
|||
} |
|||
TopModule |
|||
Read AlfaNew |
|||
Try ok { |
|||
AlfaNew.AddOne |
|||
} |
|||
\\ we get an error because M global not exist now |
|||
\\ here M is Local. |
|||
If Error or Not Ok Then Print Error$ ' Uknown M in .xz() in AlfaNew.AddOne |
|||
Print M=1000, xz()=9999 |
|||
For AlfaNew { |
|||
Global M=1234 |
|||
.ResetValues |
|||
.AddOne ' now works because M exist as global, for this block |
|||
} |
|||
Print M=1000, xz()=9999 |
|||
For This { |
|||
Local M=50 |
|||
M++ |
|||
Print M=51 |
|||
} |
|||
Print M=1000 |
|||
} |
|||
Checkit |
|||
List ' list of variables are empty |
|||
Modules ? ' list of modules show two: A and A.Checkit |
|||
Print Module$ ' print A |
|||
</lang> |
|||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |