Function definition: Difference between revisions

m
Fixed lang tags (using MediaWiki::API).
m (→‎{{header|J}}: Add lang tags)
m (Fixed lang tags (using MediaWiki::API).)
Line 7:
 
=={{header|ActionScript}}==
<lang actionscript>function multiply(a:Number, b:Number):Number {
function multiply(a:Number, b:Number):Number {
return a * b;
}</lang>
}
</lang>
 
=={{header|Ada}}==
Line 31 ⟶ 29:
 
=={{header|ALGOL 68}}==
<lang algol68>PROC multiply = ( LONG REAL a, b ) LONG REAL:
(
a * b
)</lang>
)
 
=={{header|AmigaE}}==
Line 58 ⟶ 56:
=={{header|AutoHotkey}}==
 
<lang autohotkey>MsgBox % multiply(10,2)
MsgBox % multiply(10,2)
 
multiply(multiplicand, multiplier) {
Return (multiplicand * multiplier)
}</lang>
}
</lang>
 
=={{header|AWK}}==
Line 138 ⟶ 134:
 
=={{header|Clojure}}==
<lang clojurelisp>(defn multiply [x y]
(defn multiply [x y]
(* x y))
 
(multiply 4 5)</lang>
</lang>
Or with multiple arities (in the manner of the actual <tt>*</tt> function):
<lang clojurelisp>(defn multiply
(defn multiply
([] 1)
([x] x)
Line 153 ⟶ 146:
(reduce * (* x y) more)))
 
(multiply 2 3 4 5) ; 120</lang>
</lang>
 
=={{header|Common Lisp}}==
Line 164 ⟶ 156:
=={{header|D}}==
{{works with|DMD|1.025}}
<lang d>double multiply(double a, double b)
{
return a * b;
}</lang>
}
or templated one:
<lang d>T multiply(T)(T a, T b) {
return a * b;
}</lang>
}
 
===Compile-time evaluation===
Function "multiply" can be evaluated at compile time if appears in a context where only compile-time constant is valid:
<lang d>const result = multiply(2, 3); // Evaluated at compile time
writefln("2 * 3 = ", result);</lang>
Compile-time multiplication can also be done using template:
<lang d>template multiply(int a, int b) {
const multiply = a * b;
}</lang>
}
Use it like this:
<lang d>import std.metastrings;
pragma (msg, ToString!(multiply!(2, 3))); // Prints "6" during program compilation</lang>
 
=={{header|dc}}==
For dc, the functions (called macros) are limited to names from 'a' to 'z'
Create a function called 'm'
<lang dc>[*] sm</lang>
Use it (lm loads the function in 'm',x executes it, f shows the the stack.)
<lang dc>3 4 lm x f
= 12</lang>
 
=={{header|E}}==
 
<lang e>def multiply(a, b) {
return a * b
}</lang>
}
 
(This does not necessarily return a product, but whatever the "multiply" method of <var>a</var> returns. The parameters could be guarded to only accept standard numbers.)
 
=={{header|FALSE}}==
<lang false>[*] {anonymous function to multiply the top two items on the stack}
m: {binding the function to one of the 26 available symbol names}
2 3m;! {executing the function, yielding 6}</lang>
 
=={{header|Forth}}==
<lang forth>: fmultiply ( F: a b -- F: c ) F* ;
: multiply ( a b -- c ) * ;</lang>
 
=={{header|Fortran}}==
In FORTRAN 66 or later, define a function:
<lang fortran> FUNCTION MULTIPLY(X,Y)
REAL MULTIPLY, X, Y
MULTIPLY = X * Y
END</lang>
 
In Fortran 95 or later, define an elemental function, so that this function can be applied to whole arrays as well as to scalar variables:
<lang fortran> module elemFunc
contains
elemental function multiply(x, y)
real, intent(in) :: x, y
real :: multiply
multiply = x * y
end function multiply
end module elemFunc</lang>
 
<lang fortran> program funcDemo
use elemFunc
real :: a = 20.0, b = 30.0, c
real, dimension(5) :: x = (/ 1.0, 2.0, 3.0, 4.0, 5.0 /), y = (/ 32.0, 16.0, 8.0, 4.0, 2.0 /), z
c = multiply(a,b) ! works with either function definition above
z = multiply(x,y) ! element-wise invocation only works with elemental function
end program funcDemo</lang>
 
It is worth noting that Fortran can call functions (and subroutines) using named arguments; e.g. we can call multiply in the following way:
 
<lang fortran> c = multiply(y=b, x=a) ! the same as multiply(a, b)
z = multiply(y=x, x=y) ! the same as multiply(y, x)</lang>
 
(Because of commutativity property of the multiplication, the difference between <code>multiply(x,y)</code> and <code>multiply(y,x)</code> is not evident)
Line 247 ⟶ 239:
=={{header|F_Sharp|F#}}==
The default will be an integer function but you can specify other types as shown:
<lang fsharp>let multiply x y = x * y // integer
let fmultiply (x : float) (y : float) = x * y</lang>
 
=={{header|Groovy}}==
Line 260 ⟶ 252:
 
=={{header|Haskell}}==
<lang haskell>multiply = (*)</lang>
Alternatively,
<lang haskell>multiply = \ x y -> x*y</lang>
 
=={{header|IDL}}==
Line 268 ⟶ 260:
The task description is unclear on what to do when the arguments to the function are non-scalar, so here's multiple versions:
 
<lang idl>function multiply ,a,b
return, a* b
end </lang>
 
If "a" and "b" are scalar, this will return a scalar. If they are arrays of the same dimensions, the result is an array of the same dimensions where each element is the product of the corresponding elements in "a" and "b".
Line 276 ⟶ 268:
Alternatively, there's this possibility:
 
<lang idl>function multiply ,a,b
return, product([a, b])
end </lang>
 
This will yield the same result for scalars, but if "a" and "b" are arrays it will return the product of all the elements in both arrays.
Line 284 ⟶ 276:
Finally, there's this option:
 
<lang idl>function multiply ,a,b
return, a # b
end </lang>
 
This will return a scalar if given scalars, if given one- or two-dimensional arrays it will return the matrix-product of these arrays. E.g. if given two three-element one-dimensional arrays (i.e. vectors), this will return a 3x3 matrix.
 
=={{header|Io}}==
<lang io>multiply := method(a,b,a*b)</lang>
 
=={{header|J}}==
Line 311 ⟶ 303:
 
=={{header|Joy}}==
<lang joy>DEFINE multiply == * .</lang>
DEFINE multiply == * .
</lang>
 
=={{header|Logo}}==
<lang logo>to multiply :x :y
output :x * :y
end</lang>
 
=={{header|Lucid}}==
<lang lucid>multiply(x,y) = x * y</lang>
 
=={{header|LSE64}}==
<lang lse64>multiply : *
multiply. : *. # floating point</lang>
 
=={{header|M4}}==
<lang M4>define(`multiply',`eval($1*$2)')
define(`multiply',`eval($1*$2)')
 
multiply(2,3)</lang>
</lang>
 
=={{header|Make}}==
Line 338 ⟶ 326:
with recursive make used to retrieve the returned value.
 
<lang make>A=1
B=1
 
multiply:
@expr $(A) \* $(B)</lang>
 
Invoking it
<lang make>make -f mul.mk multiply A=100 B=3
> 300</lang>
 
Using gmake, the define syntax is used to define a new function
 
<lang make>A=1
B=1
define multiply
expr $(1) \* $(2)
endef
do:
@$(call multiply, $(A), $(B))
 
(defndefine multiply [x y]
|gmake -f mul.mk do A=5 B=3
expr $(1) \* $(2)
endef
 
do:
@$(call multiply, $(A), $(B))
 
|gmake -f mul.mk do A=5 B=3</lang>
 
=={{header|MAXScript}}==
<lang maxscript>fn multiply a b =
(
a * b
)</lang>
)
 
=={{header|Metafont}}==
Line 385 ⟶ 373:
 
=={{header|Modula-3}}==
<lang modula3>PROCEDURE Multiply(a, b: INTEGER): INTEGER =
<pre>
PROCEDURE Multiply(a, b: INTEGER): INTEGER =
BEGIN
RETURN a * b;
END Multiply;</lang>
</pre>
 
=={{header|Nial}}==
Using variables
<lang nial>multiply is operation a b {a * b}</lang>
Using it
<lang nial>|multiply 2 3
=6</lang>
=6
Point free form
<lang nial>mul is *</lang>
Using it
<lang nial>|mul 3 4
=12</lang>
 
Nial also allows creation of operators
<lang nial>multiply is op a b {a * b}</lang>
Using it.
<lang nial>|2 multiply 3
=6
|multiply 2 3
=6</lang>
=6
Since this is an array programming language, any parameters can be arrays too
<lang nial>|mul 3 [1,2]
=3 6
|mul [1,2] [10,20]
=10 40</lang>
 
=={{header|Oberon-2}}==
Oberon-2 uses procedures, and has a special procedure called a "Function Procedure" used to return a value.
<lang oberon2>PROCEDURE Multiply(a, b: INTEGER): INTEGER;
<pre>
PROCEDURE Multiply(a, b: INTEGER): INTEGER;
BEGIN
RETURN a * b;
END Multiply;</lang>
</pre>
 
=={{header|OCaml}}==
Line 440 ⟶ 424:
 
=={{header|OpenEdge/Progress}}==
<lang openedge>function multiply returns dec (a as dec , b as dec ):
return a * b .
end.</lang>
 
=={{header|Pascal}}==
Line 481 ⟶ 465:
 
=={{header|PL/SQL}}==
<lang plsql>FUNCTION multiply(p_arg1 NUMBER, p_arg2 NUMBER) RETURN NUMBER
IS
v_product NUMBER;
BEGIN
v_product := p_arg1 * p_arg2;
RETURN v_product;
END;</lang>
 
=={{header|Pop11}}==
 
<lang pop11>define multiply(a, b);
a * b
enddefine;</lang>
 
=={{header|PowerShell}}==
Line 542 ⟶ 526:
 
=={{header|Q}}==
<lang q>multiply:{[a;b] a*b}</lang>
or
<lang q>multiply:{x*y}</lang>
or
<lang q>multiply:*</lang>
Using it
<lang q>multiply[2;3]
6</lang>
 
=={{header|R}}==
Line 563 ⟶ 547:
=={{header|Raven}}==
 
<lang raven>define multiply use a, b
a b *</lang>
 
Or optional infix:
 
<lang raven>define multiply use a, b
(a * b)</lang>
 
Or skip named vars:
 
<lang raven>define multiply *</lang>
 
=={{header|Ruby}}==
Line 581 ⟶ 565:
 
=={{header|Scheme}}==
<lang scheme>(define multiply *)</lang>
Alternately,
<lang scheme>(define (multiply a b)
(* a b))</lang>
 
=={{header|Seed7}}==
 
<lang seed7>const func float: multiply (in float: a, in float: b) is
return a * b;</lang>
 
=={{header|Slate}}==
<lang slate>define: #multiply -> [| :a :b | a * b].</lang>
define: #multiply -> [| :a :b | a * b].
</lang>
or using a macro:
<lang slate>define: #multiple -> #* `er.</lang>
define: #multiple -> #* `er.
</lang>
 
=={{header|Smalltalk}}==
Line 607 ⟶ 587:
=={{header|SNUSP}}==
For expediency, the function is adding two values, instead of multiplying two values. Another function, atoi (+48) is called before printing the result.
<lang snusp>+1>++2=@\=>+++3=@\==@\=.=# prints '6'
| | \=itoa=@@@+@+++++#
\=======!\==!/===?\<#
\>+<-/</lang>
 
=={{header|Standard ML}}==
<lang sml>val multiply = op *</lang>
Equivalently,
<lang sml>fun multiply (x, y) = x * y</lang>
Curried form:
<lang sml>fun multiply x y = x * y</lang>
 
=={{header|Tcl}}==
Line 638 ⟶ 618:
=={{header|TI-89 BASIC}}==
 
<lang ti89b>multiply(a, b)
Func
Return a * b
EndFunc</lang>
 
=={{header|Toka}}==
<lang toka>[ ( ab-c ) * ] is multiply</lang>
 
=={{header|Ursala}}==
Line 655 ⟶ 635:
the system library function, called using the syntax math..mul.
This is the definition in point free form,
<lang Ursala>multiply = math..mul</lang>
multiply = math..mul
</lang>
this is the definition using lambda abstraction
<lang Ursala>multiply = ("a","b"). math..mul ("a","b")</lang>
multiply = ("a","b"). math..mul ("a","b")
</lang>
and this is the definition using pattern matching.
<lang Ursala>multiply("a","b") = math..mul ("a","b")</lang>
multiply("a","b") = math..mul ("a","b")
</lang>
 
=={{header|V}}==
Line 671 ⟶ 645:
of actions supplied in the quote.
 
<lang v>[multiply *].</lang>
 
Using it
<lang v>2 3 multiply
=6</lang>
=6
 
V also allows internal bindings.
<lang v>[multiply
[a b] let
a b *].</lang>
 
 
=={{header|Visual Basic .NET}}==
 
<lang vbnet>Function Multiply(ByVal a As Integer, ByVal b As Integer) As Integer
Return a * b
End Function</lang>
 
 
Call the function
<lang vbnet>Multiply(1, 1)</lang>
 
=={{header|XSLT}}==
Templates are the closest things XSLT has to user defined functions. They can be declared to be called by name and/or to be applied to all nodes in a matching set and given "mode". Both types of template can take named parameters with default values. Templates also have a "context" node used as the base of XPath expressions (kind of like an implied "this" of an object's method).
 
<lang xslt><xsl:template name="product">
<xsl:param name="a" select="2"/>
<xsl:param name="b" select="3"/>
<fo:block>product = <xsl:value-of select="$a * $b"/></fo:block>
</xsl:template>
 
<xsl:call-template name="product">
<xsl:with-param name="a">4</xsl:with-param>
<xsl:with-param name="b">5</xsl:with-param>
</xsl:call-template></lang>
 
<xsl:call-template name="product"/> &lt;-- using default parameters of 2 and 3 -->
845

edits