Call a function: Difference between revisions
Content added Content deleted
m (→{{header|BQN}}) |
|||
Line 3,412: | Line 3,412: | ||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
=={{header|M2000 Interpreter}}== |
|||
===Standard Call of Modules/Functions=== |
|||
<pre> |
|||
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. |
|||
// Calling a function that requires no arguments |
|||
A Subroutine is code inside Modules/Functions where all definitions in module/function are visible. Modules and functions can call own modules/functions or global modules/functions. Functions can use recursion. Module with standard calling can't use recursion (need to use Call nameOfModule to call itself). |
|||
ModuleA |
|||
Call FunctionA() |
|||
Call LambdaA() |
|||
SubAlfa() ' subroutine |
|||
Print @Simple() ' simple function |
|||
// Calling a function with a fixed number of arguments |
|||
ModuleA 100 |
|||
SubAlfa(100) ' subroutine |
|||
Print @Simple(100) ' simple function |
|||
Call FunctionA(100) |
|||
Call LambdaA(100) |
|||
// Calling a function with optional arguments |
|||
ModuleA ?,4 |
|||
SubAlfa(?) ' subroutine |
|||
Print @Simple(?) ' simple function |
|||
Call FunctionA(,4) |
|||
Call LambdaA(,4) |
|||
// Calling a function with a variable number of arguments |
|||
Call FunctionA(1,2,3,4,5) |
|||
Call LambdaA(1,2,3,4,5) |
|||
// Calling a function with named arguments |
|||
ModuleA %X=10, %Z=20 |
|||
// Using a function in statement context |
|||
Module A |
|||
SubAlfa() |
|||
Call Void FunctionA() |
|||
Call Void LambdaA() |
|||
// Using a function in first-class context within an expression |
|||
Print (lambda (x,y)->{=x**y}(2,3))*8=64 |
|||
// Obtaining the return value of a function |
|||
A=FunctionA() ' numeric or object |
|||
A$=FunctionA$() ' string or object which return string |
|||
A=LambdaA() |
|||
A$=LambdaA$() |
|||
A=@SimpleA() |
|||
A$=@SimpleA$() |
|||
// Distinguishing built-in functions and user-defined functions |
|||
Def Cos(x)=100*X ' change the build in |
|||
Print Cos(3), @Cos(3) ' @Cos() is the build in anyway |
|||
' we can't use simple function with same name as a bultin function. |
|||
// Distinguishing subroutines and functions |
|||
Name without parenthesis like a command or statement is a Module |
|||
Name with parentesis without operator is a Sub |
|||
Name with parenthesis in an expression is a function or array (array come first), using a(*10) we say that it is function even an array a() exist. |
|||
Name with @ as first symbol and parenthesis like @alfa() is a simple function unless has a builtin name, so is that function. |
|||
// Stating whether arguments are passed by value or by reference |
|||
Default:By Value |
|||
By Reference: Using & at both sides (caller and calee) |
|||
Function Alfa(&x) { |
|||
=X : X++ |
|||
} |
|||
Print Alfa(&counter) |
|||
Subs and Simple Functions not need a by reference pass because they are in the same scope from the caller, so actuall we pass by value to be local to those entities. But we can use by reference, except for static variables, and array items. Those can only passed by reference on modules and nornal functions and lambdas (which are normal functions plus more). |
|||
// Is partial application possible and how |
|||
A=Lambda (X) -> { |
|||
<lang M2000 Interpreter> |
|||
=Lambda X (Y) -> { |
|||
Module CheckIt { |
|||
=Y**X |
|||
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 |
|||
\\ subs are part of modules/functions (there are no global subs, but there is a way to share definitions modules from parent module). |
|||
Module CheckSub { |
|||
M=1 |
|||
a(100) ' 400 |
|||
a(100) ' 800 |
|||
Module Child { |
|||
M=1 |
|||
a(100) ' 400 |
|||
a(100) ' 800 |
|||
} |
|||
Child |
|||
Sub a(x) |
|||
b(x*4) |
|||
M++ |
|||
End Sub |
|||
Sub b(x) |
|||
Print x*M |
|||
End Sub |
|||
} |
|||
CheckSub |
|||
</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> |
|||
===Call a reference to Function=== |
|||
<lang M2000 Interpreter> |
|||
Module Checkit { |
|||
Group Alfa { |
|||
x=10 |
|||
Function Beta { |
|||
=.x |
|||
.x++ |
|||
} |
|||
} |
|||
Module PassRef (&n()) { |
|||
Print n() |
|||
} |
|||
PassRef &Alfa.Beta() |
|||
Print Alfa.x=11 |
|||
} |
|||
Call Checkit |
|||
</lang> |
|||
===Light Event Function=== |
|||
<lang M2000 Interpreter> |
|||
Module Checkit { |
|||
Group WithEvents Alfa { |
|||
Event "AddOne" |
|||
x=10 |
|||
Function Beta { |
|||
=.x |
|||
.PrintIt .x+1 |
|||
.x++ |
|||
} |
|||
Module PrintIt (x) { |
|||
Call Event "AddOne", x |
|||
} |
|||
} |
|||
Module PassRef (&n()) { |
|||
z=n() |
|||
} |
|||
z=500 |
|||
k=0 |
|||
Function Alfa_AddOne (new z) { |
|||
\\ interpreter make a line: Read New Z |
|||
k+=z |
|||
Print z |
|||
} |
|||
PassRef &Alfa.Beta() |
|||
Print k=11, z=500 |
|||
} |
} |
||
Cube=A(3) : Square=A(2) |
|||
Call Checkit |
|||
Print Cube(2)=8, Square(2)=4 |
|||
</lang> |
|||
</pre> |
|||
=={{header|Maple}}== |
=={{header|Maple}}== |