Function definition: Difference between revisions

(14 intermediate revisions by 10 users not shown)
Line 295:
MULTIPLY := A * B;
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}}==
Line 342 ⟶ 419:
end</syntaxhighlight>
 
A function in AppleScript is called a "handler". It can take one of three different forms, depending on what the scripter finds most convenient. Calls to it must match the form used in the handler definition. The above is an example of a handler with "positional" parameters. Either <code>to</code> or <code>on</code> may be used as the first word in the headerhandler linedefinition. When the script's is compiled, the handler label is automatically appended to the <code>end</code> line too if it wasn't written in.
 
Handler names followed by zero or more parameters within parentheses are called "positional" -- the number and order of the parameters in the caller must match those in the handler definition.
 
<syntaxhighlight lang="applescript">on multiply(a, b)
Line 350 ⟶ 429:
multiply(2, 3)</syntaxhighlight>
 
AppleScript also offers handlers with "labeledprepositional" [sic]labeled parameters. These aren't used muchoften now asbecause the limited choiceset of labelAppleScript-defined enumsprepositions makes it difficult to choose ones that make sense in English, although it's just about possible. here:
 
These prepositions can be used: <code>about, above, against, apart from, around, aside from, at, below, beneath, beside, between, by, for, from, instead of, into, on, onto, out of, over, since, thru, through, and under</code>. Also, <code>of</code> is available, but if used it must be the first parameter.
 
Example:
 
<syntaxhighlight lang="applescript">on multiplication of a by b
Line 579 ⟶ 662:
 
=={{header|BASIC}}==
==={{header|ANSI BASIC}}===
{{works with|QBasic}}
In ANSI BASIC, functions can be defined as either formulas or multi-line external or internal subroutines. External functions are independent program units that can be called from within the program. Internal functions are considered part of the program unit they are contained in and can only be called from within that unit. External functions do not share any information with other program units and exchange information through parameters and returned values. Internal functions share everything with their surrounding program unit except for their parameters. Internal functions do not have local variables.
<syntaxhighlight lang="qbasic">DECLARE FUNCTION multiply% (a AS INTEGER, b AS INTEGER)
{{works with|Decimal BASIC}}
 
<syntaxhighlight lang="basic">
FUNCTION multiply% (a AS INTEGER, b AS INTEGER)
100 DEF Multiply(A, multiplyB) = aA * bB
END110 DECLARE FUNCTION</syntaxhighlight> MultiplyI
120 DECLARE EXTERNAL FUNCTION MultiplyE
130 PRINT Multiply(3, 1.23456)
140 PRINT MultiplyI(3, 1.23456)
150 PRINT MultiplyE(3, 1.23456)
160 FUNCTION MultiplyI(X, Y)
170 LET MultiplyI = X * Y
180 END FUNCTION
190 END
200 EXTERNAL FUNCTION MultiplyE(A, B)
210 LET MultiplyE = A * B
220 END FUNCTION
</syntaxhighlight>
{{out}}
<pre>
3.70368
3.70368
3.70368
</pre>
 
==={{header|Applesoft BASIC}}===
Line 687 ⟶ 788:
 
<syntaxhighlight lang="freebasic">#Define multiply(d1, d2) (d1) * (d2)</syntaxhighlight>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebasic">window 1
 
local fn multiply( a as long, b as long ) as long
end fn = a * b
 
print fn multiply( 3, 9 )
 
HandleEvents</syntaxhighlight>
Output:
<pre>
27
</pre>
 
==={{header|Gambas}}===
Line 875 ⟶ 990:
{{out}}
<pre> 3.703680038452148</pre>
 
==={{header|QuickBASIC}}===
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">DECLARE FUNCTION multiply% (a AS INTEGER, b AS INTEGER)
 
FUNCTION multiply% (a AS INTEGER, b AS INTEGER)
multiply = a * b
END FUNCTION</syntaxhighlight>
 
==={{header|REALbasic}}===
Line 964 ⟶ 1,087:
return a * b
end sub</syntaxhighlight>
 
==={{header|Xojo}}===
<syntaxhighlight lang="vbnet">Function Multiply(ByVal a As Integer, ByVal b As Integer) As Integer
Return a * b
End Function</syntaxhighlight>
Call the function
<syntaxhighlight lang="vbnet">Dim I As Integer = Multiply(7, 6)</syntaxhighlight>
 
==={{header|ZX Spectrum Basic}}===
Line 1,019 ⟶ 1,149:
 
print multiply(3, 2)</syntaxhighlight>
 
=={{header|Binary Lambda Calculus}}==
 
In lambda calculus, multiplication on Church numerals is <code>mul = \m \n \f. m (n f)</code> which in BLC is
 
<pre>00 00 00 01 1110 01 110 10</pre>
 
If mul is used several times within an expression E, then they can share the same definition by using <code>(\mul. E)(\m\n\f. m (n f))</code>. For example, the cube function is <code>\n. (\mul. mul n (mul n n)) (\m\n\f. m (n f))</code> which in BLC is
 
<pre>00 01 00 01 01 10 110 01 01 10 110 110 0000000111100111010</pre>
 
=={{header|BQN}}==
Line 1,394 ⟶ 1,534:
return a * b
.
print multiply 7 5
</syntaxhighlight>
 
Line 1,476 ⟶ 1,616:
= a * b;</syntaxhighlight>
Anonymous function / closure:
<syntaxhighlight lang="elena">symbol f := (x,y => x * y);</syntaxhighlight>
Root closure:
<syntaxhighlight lang="elena">f(x,y){ ^ x * y }</syntaxhighlight>
Line 1,734 ⟶ 1,874:
let multiply (x: i32, y: i32) : i32 = x * y
</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">window 1
 
local fn multiply( a as long, b as long ) as long
end fn = a * b
 
print fn multiply( 3, 9 )
 
HandleEvents</syntaxhighlight>
Output:
<pre>
27
</pre>
 
=={{header|GAP}}==
Line 2,057 ⟶ 2,183:
 
=={{header|langur}}==
Langur functions are first-order. They are pure in terms of setting values and in terms of I/O (unless declared impure).
A function body may use curly braces, but it is not required if it is a single expression.
 
A return statement may be used, but a function's last value is its implicit return value.
 
=== parameters ===
Functions defined with explicit parameters may be closures, and those defined with implied parameters are not.
Parameters are defined within parentheses after the fn token. To specify no parameters, use an empty set of parentheses.
 
<syntaxhighlight lang="langur">val .multiply = fn(.x, .y) { .x * .y }
Langur functions are first-order. They are pure in terms of setting values, though not in terms of I/O.
 
=== explicit parameters ===
Explicit parameters are defined with parentheses after the f token, with no spacing. To specify no parameters, use an empty set of parentheses.
<syntaxhighlight lang="langur">val .multiply = f(.x, .y) .x x .y
.multiply(3, 4)</syntaxhighlight>
 
=== implied parameters ===
Parameters are implied when the f token is not immediately followed by parentheses without spacing. The implied order of implied parameters is based on the string sort order of their names, not their order within the function.
<syntaxhighlight lang="langur">val .multiply = f .x x .y
.multiply(3, 4)</syntaxhighlight>
 
=== operator implied functions ===
Operator implied functions are built using an infix operator between curly braces on an ffn token.
 
<syntaxhighlight lang="langur">val .multiply = fn{*}
{{works with|langur|0.6.6}}
<syntaxhighlight lang="langur">val .multiply = f{x}
.multiply(3, 4)</syntaxhighlight>
 
=== nil left partially implied functions ===
These are built with an infix operator and onea right-hand operand inside the ffn{...} tokens.
 
<syntaxhighlight lang="langur">val .times3 = fn{* 3}
{{works with|langur|0.8.11}}
<syntaxhighlight lang="langur">val .times3 = f{x 3}
map .times3, [1, 2, 3]</syntaxhighlight>
 
=== impure functions (I/O) ===
Impure functions must be declared as such.
<syntaxhighlight>val .writeit = impure fn(.x) { writeln .x }</syntaxhighlight>
 
Impure functions cannot be passed to pure functions.
 
=={{header|Lasso}}==
Line 3,414 ⟶ 3,535:
The following 'multiply' function will work for any type(s) that support the '*' operator.
However, it will produce a runtime error otherwise, as demonstrated by the final example.
<syntaxhighlight lang="ecmascriptwren">var multiply = Fn.new { |a, b| a * b }
 
System.print(multiply.call(3, 7))
Line 3,516 ⟶ 3,637:
<syntaxhighlight lang="scheme">(define multiply
(lambda (x y) (* x y)))</syntaxhighlight>
 
=={{header|Xojo}}==
<syntaxhighlight lang="vbnet">Function Multiply(ByVal a As Integer, ByVal b As Integer) As Integer
Return a * b
End Function</syntaxhighlight>
Call the function
<syntaxhighlight lang="vbnet">Dim I As Integer = Multiply(7, 6)</syntaxhighlight>
 
=={{header|XPL0}}==
Line 3,597 ⟶ 3,711:
pop bc
ret</syntaxhighlight>
 
=={{header|zig}}==
<syntaxhighlight lang="zig">fun multiply(x: i64, y: i64) i64 {
return x * y;
}
 
//example call
const x: i64 = 4;
const y: i64 = 23;
_ = multipy(x, y); // --> 93</syntaxhighlight>
 
=={{header|zkl}}==
885

edits