Function prototype: Difference between revisions
Content added Content deleted
(→{{header|REXX}}: added the REXX computer programming language.) |
No edit summary |
||
Line 686: | Line 686: | ||
function anyargs(xs: ...): int = ? ;; |
function anyargs(xs: ...): int = ? ;; |
||
function plusargs(x:int, xs: ...): int = ? ;;</lang> |
function plusargs(x:int, xs: ...): int = ? ;;</lang> |
||
=={{header|M2000 Interpreter}}== |
|||
Functions/modules are declared before used. So the flow matter, position in code not matter (perhaps we can put functions in a simple routine, using a label, execute a gosub to label, then make the functions, and then return. Functions amd modules added to a specific list of functions/modules, so every time interpreter check that list (a hash table). They can change definition including signature. Any module/function before executed has no declared local modules/functions. Declarations performed as they executed. For modules, we can prepare a non changed module before begin execute module's code, and when declaration for same name module comes to execute, it just skipped. |
|||
Subroutines are parts of functions/modules and first searched from bottom, added to a list of subs positions,therefore they can't changed. |
|||
Example of change an inner module using another module with same signature. Module MyBeta {Read x : ... } or Module MyBeta (x) { ... } or Module MyBeta(x) { } is the same. |
|||
<lang M2000 Interpreter> |
|||
Module Check { |
|||
Module MyBeta (a) { |
|||
Print "MyBeta", a/2 |
|||
} |
|||
Module TestMe { |
|||
Module Beta (x) { |
|||
Print "TestMeBeta", x |
|||
} |
|||
Beta 100 |
|||
} |
|||
TestMe ; Beta as MyBeta |
|||
} |
|||
Check |
|||
</lang> |
|||
Signatures needed for Event object. An event object get a list of functions, called as modules, and call every function with same signature. We can provide arguments by reference too. We can define simple functions (without visibility except local and global), or group functions (static groups) with visibility local, global and group level, or we can define local scope functions. |
|||
<lang M2000 Interpreter> |
|||
Module Check {, |
|||
\\ make an event object |
|||
\\ with a prototype signature |
|||
\\ first parameter is numeric/object by value, and second is by reference |
|||
Event Alfa { |
|||
Read x, &z |
|||
} |
|||
\\ make a function with same signature |
|||
Function ServiceAlfa { |
|||
read a, &b |
|||
b+=a |
|||
} |
|||
\\ add function to event |
|||
Event Alfa new &ServiceAlfa() |
|||
\\ call event in this module |
|||
var=30 |
|||
Call Event Alfa, 10, &var |
|||
Print var=40 |
|||
\\ make a local module, and pass event by value |
|||
Module checkinside (ev) { |
|||
\\ ev is a copy of Alfa |
|||
m=10 |
|||
call event ev, 4, &m |
|||
Print m=14 |
|||
\\ clear functions from ev |
|||
Event ev Clear |
|||
\\ we can call it again, but nothing happen |
|||
call event ev, 4, &m |
|||
Print m=14 |
|||
} |
|||
checkinside Alfa |
|||
\\ so now we call Alfa |
|||
Call Event Alfa, 10, &var |
|||
Print var=50 |
|||
Event Alfa Hold |
|||
\\ calling do nothing, because of Hold state |
|||
Call Event Alfa, 10, &var |
|||
Event Alfa Release |
|||
Call Event Alfa, 10, &var |
|||
Print var=60 |
|||
} |
|||
Check |
|||
</lang> |
|||
Using a function for local call (module visibility) |
|||
<lang M2000 Interpreter> |
|||
Module Check {, |
|||
\\ make an event object |
|||
\\ with a prototype signature |
|||
\\ first parameter is numeric/object by value, and second is by reference |
|||
Event Alfa { |
|||
Read x, &z |
|||
} |
|||
\\ make a function with same signature |
|||
\\ but here prepared to used with current module visibility |
|||
m=0 |
|||
Function ServiceAlfa { |
|||
\ this code "see" m variable |
|||
\\ we have to use new, to make new a, b for sure |
|||
read new a, &b |
|||
b+=a |
|||
m++ |
|||
} |
|||
\\ add function to event, making reference as local to module |
|||
Event Alfa new Lazy$(&ServiceAlfa()) |
|||
\\ call event in this module |
|||
var=30 |
|||
Call Event Alfa, 10, &var |
|||
Print var=40 |
|||
\\ make a local module, and pass event by value |
|||
Module checkinside (ev) { |
|||
\\ ev is a copy of Alfa |
|||
m=10 |
|||
call event ev, 4, &m |
|||
Print m=14 |
|||
\\ clear functions from ev |
|||
Event ev Clear |
|||
\\ we can call it again, but nothing happen |
|||
call event ev, 4, &m |
|||
Print m=14 |
|||
} |
|||
checkinside Alfa |
|||
\\ so now we call Alfa |
|||
Call Event Alfa, 10, &var |
|||
Print var=50 |
|||
Event Alfa Hold |
|||
\\ calling do nothing, because of Hold state |
|||
Call Event Alfa, 10, &var |
|||
Event Alfa Release |
|||
Call Event Alfa, 10, &var |
|||
Print var=60 |
|||
Print m=4 ' 4 times called ServiceAlfa |
|||
} |
|||
Check |
|||
</lang> |
|||
Using a Function in a Group (Groups are the User objects in M2000) |
|||
<lang M2000 Interpreter> |
|||
Module Check {, |
|||
\\ make an event object |
|||
\\ with a prototype signature |
|||
\\ first parameter is numeric/object by value, and second is by reference |
|||
Event Alfa { |
|||
Read x, &z |
|||
} |
|||
\\ make a group function with same signature |
|||
Group IamStatic { |
|||
m=0 |
|||
Function ServiceAlfa(a, &b) { |
|||
b+=a |
|||
.m++ |
|||
} |
|||
} |
|||
\\ add function to event, making reference as local to module |
|||
Event Alfa new &IamStatic.ServiceAlfa() |
|||
\\ call event in this module |
|||
var=30 |
|||
Call Event Alfa, 10, &var |
|||
Print var=40 |
|||
\\ make a local module, and pass event by value |
|||
Module checkinside (ev) { |
|||
\\ ev is a copy of Alfa |
|||
m=10 |
|||
call event ev, 4, &m |
|||
Print m=14 |
|||
\\ clear functions from ev |
|||
Event ev Clear |
|||
\\ we can call it again, but nothing happen |
|||
call event ev, 4, &m |
|||
Print m=14 |
|||
} |
|||
checkinside Alfa |
|||
\\ so now we call Alfa |
|||
Call Event Alfa, 10, &var |
|||
Print var=50 |
|||
Event Alfa Hold |
|||
\\ calling do nothing, because of Hold state |
|||
Call Event Alfa, 10, &var |
|||
Event Alfa Release |
|||
Call Event Alfa, 10, &var |
|||
Print var=60 |
|||
Print IamStatic.m=4 ' 4 times called IamStatic.ServiceAlfa |
|||
} |
|||
Check |
|||
</lang> |
|||
=={{header|Nim}}== |
=={{header|Nim}}== |