Function definition: Difference between revisions
Content added Content deleted
imported>Arakov |
|||
Line 295: | Line 295: | ||
MULTIPLY := A * B; |
MULTIPLY := A * B; |
||
END;</syntaxhighlight> |
END;</syntaxhighlight> |
||
=={{header|Amazing Hopper}}== |
|||
Hopper has no functions, but they can be declared with macros, which are resolved at compile time. Access to the working stack is global, but "local" variables can be declared in program segments written after the ".locals" clause. Let's look at some examples of declaring "functions". |
|||
<syntaxhighlight lang="c"> |
|||
/* this need data into stack */ |
|||
#context Multiplication |
|||
mul |
|||
Return \\ |
|||
#synon Multiplication *getproduct |
|||
#context-free anothermul |
|||
/* #defn Args(*) #GENCODE $$$*$$$ #REVLIST=0,mov(#REVLIST);#ENDGEN, */ |
|||
Args 'a,b' |
|||
Return ( #(a*b) )\\ |
|||
#synon anothermul *getanotherproduct |
|||
#include <jambo.h> |
|||
#prototype _multiply(_X_,_Y_) |
|||
#synon __multiply Multiply |
|||
Main |
|||
/* "prototipos" of functions and procedures. |
|||
Solves internaly */ |
|||
Printnl ( Multiply ( 10, 4 ) ) |
|||
Printnl ( __multiply ( 10, 4 ) ) |
|||
/* definición alternativa 1 */ |
|||
Printnl ( Set' 10,4 ', Gosub ' Multiply2 ') |
|||
/* aseembler Hopper 1 */ |
|||
{10,4} jsub( Multiply3 ), {"\n"} print |
|||
/* assembler Hopper 2 */ |
|||
{10,4} jsub( Multiply4 ), {"\n"} print |
|||
/* context */ |
|||
Set '10,4', now get product, and print with newline |
|||
/* context-free */ |
|||
Set '10,4', and get another product; then print with newline |
|||
End |
|||
.locals /* Subrutines */ |
|||
_multiply(a,b) |
|||
Return ( Mul(a,b) ) |
|||
/* Define is macro. Others macros: Function, Procedure: |
|||
#defn Define(_F_,*) _F_:,#GENCODE $$$*$$$ #REVLIST=0;mov(#REVLIST);#ENDGEN; |
|||
#defn Function(_F_,*) _F_:,#GENCODE $$$*$$$ #REVLIST=0;mov(#REVLIST);#ENDGEN; |
|||
#defn Procedure(_F_,*) _F_:,#GENCODE $$$*$$$ #REVLIST=0;mov(#REVLIST);#ENDGEN; |
|||
*/ |
|||
Define 'Multiply2, a,b' |
|||
Return ( Mul(a,b) ) |
|||
Multiply3: |
|||
b=0, mov(b), a=0, mov(a) |
|||
{a,b}mul /* result into stack */ |
|||
Return |
|||
Multiply4: |
|||
mul /* get values from stack, |
|||
and put result into stack */ |
|||
back /* Return */ |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
40.000000 |
|||
40.000000 |
|||
40.000000 |
|||
40.000000 |
|||
40.000000 |
|||
40.000000 |
|||
40.000000 |
|||
</pre> |
|||
=={{header|AmigaE}}== |
=={{header|AmigaE}}== |