Call a function: Difference between revisions

no edit summary
No edit summary
Line 2,392:
/* Is partial application possible and how */
tasty_curry(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)(m)(n)(o)(p)(q)(r)(s)(t)(u)(v)(w)(x)(y)(z);;</lang>
 
=={{header|M2000 Interpreter}}==
In M2000 we can use arrays, functions and subroutines with same name. Using @ we can direct interpreter to use function. Using Gosub we direct interpreter to call a subroutine. These happen at module/function level.
 
<lang M2000 Interpreter>
Module CheckIt {
Dim a(4)=100
Def a(x)=x**2
Print a(2), a(@2)
Gosub a(4)
Print "End"
Sub a(x)
Print "This is sub a()", x
End Sub
}
CheckIt
Call CheckIt
\\ both module and function can exist with same name
Function CheckIt {
Dim a(4)=100
Def a(x)=x**2
Print a(2), a(@2)
Gosub a(4)
Print "End"
Sub a(x)
Print "This is sub a()", x
End Sub
}
Call CheckIt()
Call Function Checkit
\\ if a function return a non zero number then we get error with value the returned number. Using Void we drop any return value, so no error happen.
Call Void CheckIt()
Call Void Function Checkit
</lang>
 
===Call Local===
Using standard call, a call to a module or a function open a new namespace, so we can use local variables. But what if we want to call a function as a subroutine, where the namespace is the same as the module/function where we define it.
So we have to ''read new'' to make K new, not reading the same K in module Checkit. We have to use Local N to shadow N in module Checkit. After the call to Alfa, all new definitions erased. The sane hold for call Checkit, anything defined there erased after the call. Only Stack return. A Call Local use new stack, the same as the calling of function in expression (a standard Call to module or function, using Call statement use parent stack)
 
<lang M2000 Interpreter>
Module CheckIt {
M=100
K=5
N=200
Function Alfa {
Read New K
Local N=3
M++
Print M*K/3
}
Call Local Alfa(30) ' print 1010
Print M=101, K=5, N=200
}
CheckIt
</lang>
 
===Using Event Object===
We have to define the type of arguments to event object. We can add functions to event and we can remove them, except one internal (optional defined using Function {} without a read command)
<lang M2000 Interpreter>
Module CheckIt (&P){
Event Alfa {
Read X, &M
Function {
Print X, M
}
}
Function Other (a, &b) {
b++
Print a*b
}
Event Alfa New &Other()
Call Event Alfa, 3, &P
\\ Print 3 10 \\ form internal function in Event Alfa
\\ Print 33 \\ from Other()
Print P=11
Push ALfa
}
M=10
Checkit &M
Read ReturnedEventObject
Call Event ReturnedEventObject, 2, &M
\\ Print 2 11 \\ form internal function in Event Alfa
\\ Print 24 \\ from Other(), which is a closure to event
Print M=12
</lang>
 
 
=={{header|Maple}}==
Anonymous user