RosettaCodeData/Task/Nested-function/M2000-Interpreter/nested-function.m2000

76 lines
1.9 KiB
Plaintext

Module Checkit {
Make_List(". ")
Sub Make_List(Separator$)
Local Counter=0
Make_Item("First")
Make_Item("Second")
Make_Item("Third")
End Sub
Sub Make_Item(Item_Name$)
Counter++
Print Str$(Counter,"")+Separator$+Item_Name$
End Sub
}
Checkit
Module Make_List {
Global Counter=0, Separator$=Letter$
Make_Item("First")
Make_Item("Second")
Make_Item("Third")
Sub Make_Item(Item_Name$)
Counter++
Print Str$(Counter,"")+Separator$+Item_Name$
End Sub
}
Make_List ". "
Module Make_List1 {
Global Counter=0, Separator$=Letter$
Module Make_Item (Item_Name$) {
Counter++
Print Str$(Counter,"")+Separator$+Item_Name$
}
Make_Item "First"
Make_Item "Second"
Make_Item "Third"
}
Make_List1 ". "
Module Make_List (Separator$) {
Def Counter as Integer
// Need New before Item_Name$, because the scope is the module scope
// the scope defined from the calling method.
// by default a function has a new namespace.
Function Make_Item(New Item_Name$){
Counter++
Print Str$(Counter,"")+Separator$+Item_Name$
}
// Call Local place the module scope to function
// function called like a module
Call Local Make_Item("First")
Call Local Make_Item("Second")
Call Local Make_Item("Third")
Print "Counter=";Counter // 3
}
Make_List ". "
Module Make_List (Separator$) {
Def Counter
// using Module not Function.
Module Make_Item(New Item_Name$){
Counter++
Print Str$(Counter,"")+Separator$+Item_Name$
}
Call Local Make_Item,"First"
Call Local Make_Item,"Second"
Call Local Make_Item,"Third"
Print "Counter=";Counter // 3
}
Make_List ". "