Function definition: Difference between revisions
Content deleted Content added
No edit summary |
m →ES6-* |
||
(41 intermediate revisions by 28 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
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 "
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}}===
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.
{{works with|Decimal BASIC}}
<syntaxhighlight lang="basic">
100 DEF Multiply(A,
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 889 ⟶ 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 900 ⟶ 1,009:
S-BASIC is unusual in that the function return value is assigned to the END statement that terminates the function.
<syntaxhighlight lang="basic">
function multiply(a, b =
end = a * b
rem - exercise the function
print "The product of 9 times 3 is"; multiply(9, 3)
end
</syntaxhighlight>
{{out}}
<pre>
The product of 9 times 3 is 27
</pre>
==={{header|SmallBASIC}}===
<syntaxhighlight lang="qbasic">
func multiply(a, b)
return a * b
end
print "2 * 3 = "; multiply(2, 3)
</syntaxhighlight>
If function definition is only one line, <code>def</code> can be used:
<syntaxhighlight lang="qbasic">
def multiply(a, b) = a * b
print "2 * 3 = "; multiply(2, 3)
</syntaxhighlight>
==={{header|True BASIC}}===
Line 968 ⟶ 1,103:
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,023 ⟶ 1,165:
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,083 ⟶ 1,235:
return a*b;
}</syntaxhighlight>
=={{header|Chapel}}==
<syntaxhighlight lang="text">
proc multiply(a, b)
{
return a * b;
}
</syntaxhighlight>
Can require that the two arguments be of the same type.
<syntaxhighlight lang="text">
proc multiply(a : ?t ... 2)
{
return a(0) * a(1)
}
</syntaxhighlight>
Will work on any type where the * operator is defined.
=={{header|ChucK}}==
Line 1,131 ⟶ 1,302:
=={{header|COBOL}}==
In COBOL, ''multiply'' is a reserved word, so the requirements must be relaxed to allow a different function name.
{{
The following uses a subprogram:
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. myTest.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 x
01 y
01 z
PROCEDURE DIVISION.
CALL "myMultiply" USING
Line 1,152 ⟶ 1,324:
DATA DIVISION.
LINKAGE SECTION.
01 x
01 y
01 z
PROCEDURE DIVISION USING BY REFERENCE x, y, z.
MULTIPLY x BY y GIVING z.
EXIT PROGRAM.
END PROGRAM myMultiply.</syntaxhighlight>
{{Works with|COBOL 2002}}
This example uses user-defined functions.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. myTest.
Line 1,170 ⟶ 1,342:
DATA DIVISION.
WORKING-STORAGE SECTION.
01 x
01 y
PROCEDURE DIVISION.
DISPLAY myMultiply(x, y).
Line 1,181 ⟶ 1,353:
DATA DIVISION.
LINKAGE SECTION.
01 x
01 y
01 z
PROCEDURE DIVISION USING x, y RETURNING z.
MULTIPLY x BY y GIVING z.
END FUNCTION myMultiply.</syntaxhighlight>
Line 1,203 ⟶ 1,375:
=={{header|ColdFusion}}==
====Tag style====
<syntaxhighlight lang="coldfusion"><cffunction name="multiply" returntype="numeric">
<cfargument name="a" type="numeric">
Line 1,208 ⟶ 1,381:
<cfreturn a * b>
</cffunction></syntaxhighlight>
====Script style====
<syntaxhighlight lang="lisp">numeric function multiply(required numeric a, required numeric b){
return a * b;
}
</syntaxhighlight>
=={{header|Common Lisp}}==
Line 1,334 ⟶ 1,514:
me_msg()_funct(multiply)_param(1,2);</syntaxhighlight>
=={{header|DM}}==
Functions (called procs) may be derived from <code>proc</code>.
<syntaxhighlight lang="dm">proc/multiply(a, b)
return a * b
</syntaxhighlight>
=={{header|Draco}}==
Line 1,379 ⟶ 1,565:
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
func
return a * b
.
=={{header|EchoLisp}}==
Line 1,416 ⟶ 1,603:
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
module MultiplyExample {
static <Value extends Number> Value multiply(Value n1, Value n2) {
return n1 * n2;
void run() {
(Int i1, Int i2) = (7, 3);
Int i3 = multiply(i1, i2);
Line 1,431 ⟶ 1,615:
@Inject Console console;
console.print($"{i1}*{i2}={i3}, {d1}*{d2}={d3}");
}
}
</syntaxhighlight>
Line 1,467 ⟶ 1,651:
= a * b;</syntaxhighlight>
Anonymous function / closure:
<syntaxhighlight lang="elena">symbol f
Root closure:
<syntaxhighlight lang="elena">f(x,y){ ^ x * y }</syntaxhighlight>
Line 1,507 ⟶ 1,691:
"Return the product of X and Y."
(* x y))</syntaxhighlight>
=={{header|EMal}}==
<syntaxhighlight lang="emal">
fun multiply = var by var a, var b
return a * b
end
writeLine(multiply(6, 7))
writeLine(multiply("can", 2))
</syntaxhighlight>
{{out}}
<pre>
42
cancan
</pre>
=={{header|Erlang}}==
Line 1,858 ⟶ 2,056:
public static int multiply( int a, int b) { return a*b; }
public static double multiply(double a, double b) { return a*b; }
}
</syntaxhighlight>
===Using Java version 8===
Since version 8, Java has various built in Functions with their normal mathematical names.
<syntaxhighlight lang="java">
import java.util.function.BiFunction;
import java.util.function.DoubleUnaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
public final class FunctionDefinition {
public static void main(String[] args) {
System.out.println(size.apply("Rosetta"));
System.out.println(greeting.apply("Joe"));
System.out.println(half.applyAsDouble(7));
System.out.println(lessThanTen.test(15));
System.out.println(add.apply(2, 3));
}
private static Function<String, Integer> size = s -> s.length();
private static UnaryOperator<String> greeting = s -> "Hello " + s;
private static DoubleUnaryOperator half = a -> a / 2;
private static Predicate<Integer> lessThanTen = a -> a < 10;
private static BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
}
</syntaxhighlight>
{{ out }}
<pre>
7
Hello Joe
3.5
false
5
</pre>
=={{header|JavaScript}}==
Line 1,901 ⟶ 2,140:
<syntaxhighlight lang="javascript">var multiply = (a, b) => a * b;
var multiply = (a, b) => { return a * b };
// Or, `var` being long deprecated, and currying convenient,
// (particularly in use with the higher-order Array methods)
const multiply = a =>
b => a * b;
</syntaxhighlight>
Line 1,988 ⟶ 2,233:
</syntaxhighlight>
=={{header|
=== Function decleration ===
<syntaxhighlight lang="lang">
fp.multiply = ($a, $b) -> {
return parser.op($a * $b)
}
</syntaxhighlight>
=== One-line function decleration ===
<syntaxhighlight lang="lang">
fp.multiply = ($a, $b) -> return parser.op($a * $b)
</syntaxhighlight>
=== Function decleration by using operator functions ===
<syntaxhighlight lang="lang">
fp.multiply = fn.mul
</syntaxhighlight>
=== Function decleration by using combinator functions ===
Combinator functions can be called partially, fn.argCnt2 is used to force the caller to provide 2 arguments to prevent partially calling fp.multiply
<syntaxhighlight lang="lang">
fp.multiply = fn.argCnt2(fn.combA2(fn.mul))
</syntaxhighlight>
=== Function decleration with call by pointer ===
<syntaxhighlight lang="lang">
return parser.op($*a * $*b) # Pointers can be dereferenced by using *
}
</syntaxhighlight>
=={{header|langur}}==
<syntaxhighlight>
<syntaxhighlight>
val multiply = fn a, b: a * b
</syntaxhighlight>
<syntaxhighlight>
=={{header|Lasso}}==
Line 2,058 ⟶ 2,314:
multiply call (2, 3).
multiply call: 2, 3.</syntaxhighlight>
=={{header|LDPL}}==
<syntaxhighlight lang="ldpl">data:
n is number
procedure:
sub multiply
parameters:
x is number
y is number
result is number
procedure:
in result solve x * y
end sub
# call the bare sub-procedure
call multiply with 3 4 n
display n lf
# create a statement for it
create statement "multiply $ by $ in $" executing multiply
multiply 3 by 4 in n
display n lf
</syntaxhighlight>
{{out}}
<pre>
12
12
</pre>
=={{header|LFE}}==
Line 2,601 ⟶ 2,887:
After a <tt>function</tt> has been activated, there must have be ''exactly one'' assignment to the (implicitly declared) variable bearing the same name as of the function.
Many processors do not comply with this specification, though, and allow ''overwriting'' the return value ''multiple'' times.
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
function multiply(a,b: real): real := a + b;
</syntaxhighlight>
=={{header|Perl}}==
Line 2,935 ⟶ 3,226:
};</syntaxhighlight>
=={{header|RPL}}==
≪ * ≫ 'MULT' STO
2 3 MULT
{{out}}
<pre>6</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">def multiply(a, b)
a * b
end</syntaxhighlight>
Ruby 3.0 adds endless method definition:
<syntaxhighlight lang="ruby">def multiply(a, b) = a * b</syntaxhighlight>
=={{header|Rust}}==
Line 2,954 ⟶ 3,252:
end;
end;</syntaxhighlight>
=={{header|Scala}}==
Line 3,189 ⟶ 3,466:
}</syntaxhighlight>
=={{header|Symstatic}}==
You can write functions in let expressions or anonimously:
<syntaxhighlight lang="symstatic">let prod(a, b) := a*b
let prodAnon := (a, b) -> a*b
assert(prodAnon(2, 2) = ((a, b) -> a*b)(2, 2))
assert(prod(2, 2) = prodAnon(2, 2))
</syntaxhighlight>
=={{header|Tcl}}==
Strictly as described in the task:
Line 3,281 ⟶ 3,567:
[a b] let
a b *].</syntaxhighlight>
=={{header|V (Vlang)}}==
<syntaxhighlight lang="Zig">
fn multiply(a f64, b f64) f64 {
return a * b
}
fn main() {
print(multiply(5, 6))
}
</syntaxhighlight>
{{out}}
<pre>
30.0
</pre>
=={{header|Wart}}==
Line 3,314 ⟶ 3,616:
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="
System.print(multiply.call(3, 7))
Line 3,416 ⟶ 3,718:
<syntaxhighlight lang="scheme">(define multiply
(lambda (x y) (* x y)))</syntaxhighlight>
=={{header|XPL0}}==
Line 3,458 ⟶ 3,753:
<syntaxhighlight lang="xslt">{mf:multiply(2,3)}
<xsl:value-of select="mf:multiply(2,3)" /></syntaxhighlight>
=={{header|YAMLScript}}==
<syntaxhighlight lang="yaml">
!yamlscript/v0
# Main function definition with variable arguments:
defn main(& args):
answer =:
multiply: ARGS*
say: "multiply($(ARGS.join(', '))) = $answer"
# A multi-arity function definition:
defn multiply:
(): 1
(x): x
(x y): x * y
(x y & more):
reduce multiply: multiply(x y) more
</syntaxhighlight>
=={{header|Yorick}}==
Line 3,497 ⟶ 3,811:
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}}==
|