Scope/Function names and labels: Difference between revisions
Content added Content deleted
(Add Factor) |
No edit summary |
||
Line 597: | Line 597: | ||
Good-bye! |
Good-bye! |
||
</pre> |
</pre> |
||
=={{header|M2000 Interpreter}}== |
|||
There are 5 structures for named routines. |
|||
The Module (as a procedure) and the Function which use blocks {}. Each one define a scope. All entities defined inside erased, except the static types. We can define global modules/functions in any module or function. By default we define Local. We can't call something that is no global, or no as 1st child, or not a member of an object (if a module or function is a member of an object). Global names shadow any same name in the modules/function list, or if it is variable or array in the variable/array list. We can change the code of any module or function using a newer definition, except for those which are members of object and are marked as Final. In objects, modules and functions as members may be public or private. A private member is not private in a module or function as a member of the same type. Special case are the Operators for objects, these are private, except for expression evaluator. Modules and Functions may have static variables. Static variables are common for each call in a recursion type call. Static variables may differ as values for same function, because they saved to the caller object. |
|||
In a module or function we can bound at the end definitions, named Subroutines, and simple Functions. These definitions not used block and are at the end of the module's code or function's code. The simple functions called with @ before. The subs called as NameWithParenthesis(). We can define local variables, array, for temporary use modules and functions too. |
|||
The lambda function has a Variable name and a Function name. We can pass it as a value, so each lambda function because it is a function has own scope. Closures in a lambda are copied values. In a recursion call for lambda (we call it using Lambda()) the closures are common. Here we don't have connection to the caller object. |
|||
M2000 can make references to Functions, to Lambda Function but not for simple functions (which are bound to modules and functions). Lambda functions and Functions as members of objects can be passed by reference and they have access to object members. |
|||
Labels can be used in a module or function. Numbers or names. Names need a line for them, but numbers can exist with statements |
|||
There are statements Goto and Gosub as in Basic, including On Goto and On Gosub. We can make simple routines using Return to return from them.We can jump out from a block of code. We can't do the opposite, to enter in a block of code from outside. We can't jump out of Module or Function block, we can exit only using Exit or Break statements (Break exit on multiple blocks, Exit exit the current block). So Goto used to exit from a specific inner block to some block above it. |
|||
<lang M2000 Interpreter> |
|||
Function Master { |
|||
Module Alfa { |
|||
Gosub 100 |
|||
Global M=1000 |
|||
\\ delta print 1000 |
|||
delta |
|||
End |
|||
100 Print Module(Beta)=False |
|||
Print Module(Delta)=True |
|||
Return |
|||
} |
|||
Group Object1 { |
|||
Function Master { |
|||
=M |
|||
} |
|||
Module Final Beta { |
|||
\\ delta print 500 |
|||
delta |
|||
alfa() |
|||
Sub alfa() |
|||
Local N=@Kappa(3) |
|||
Global M=N |
|||
\\ delta print 1500 |
|||
Delta |
|||
Print This.Master()=1500 |
|||
N=@Kappa(6) |
|||
\\ change value of M, not shadow M like Global M |
|||
M<=N |
|||
\\ delta print 900 |
|||
Delta |
|||
Print This.Master()=9000 |
|||
End Sub |
|||
Function Kappa(K) |
|||
=M*K |
|||
End Function |
|||
} |
|||
} |
|||
Module Global Delta { |
|||
Goto name1 |
|||
\\ a remark here |
|||
name1: |
|||
Print Module(Alfa)=False |
|||
Print Module(Beta)=False |
|||
Print Module(Delta)=True |
|||
Print M |
|||
} |
|||
\\ This is the program |
|||
K=100 |
|||
Global M=500 |
|||
Alfa |
|||
Object1.Beta |
|||
Print Object1.Master()=500 |
|||
Print K=100, M=500 |
|||
} |
|||
Call Master() |
|||
\\ No variables exist after the return from Master() |
|||
Print Valid(M)=False |
|||
</lang> |
|||
=={{header|Oforth}}== |
=={{header|Oforth}}== |