Function definition: Difference between revisions
m
Fixed lang tags (using MediaWiki::API).
m (→{{header|J}}: Add lang tags) |
Underscore (talk | contribs) m (Fixed lang tags (using MediaWiki::API).) |
||
Line 7:
=={{header|ActionScript}}==
<lang actionscript>function multiply(a:Number, b:Number):Number {
return a * b;
}</lang>▼
▲</lang>
=={{header|Ada}}==
Line 31 ⟶ 29:
=={{header|ALGOL 68}}==
)</lang>▼
=={{header|AmigaE}}==
Line 58 ⟶ 56:
=={{header|AutoHotkey}}==
<lang autohotkey>MsgBox % multiply(10,2)
multiply(multiplicand, multiplier) {
Return (multiplicand * multiplier)
}</lang>▼
▲</lang>
=={{header|AWK}}==
Line 138 ⟶ 134:
=={{header|Clojure}}==
<lang
(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
([] 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>
or templated one:
}</lang>▼
===Compile-time evaluation===
Function "multiply" can be evaluated at compile time if appears in a context where only compile-time constant is valid:
Compile-time multiplication can also be done using template:
}</lang>▼
Use it like this:
=={{header|dc}}==
For dc, the functions (called macros) are limited to names from 'a' to 'z'
Create a function called 'm'
Use it (lm loads the function in 'm',x executes it, f shows the the stack.)
=={{header|E}}==
}</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}}==
=={{header|Forth}}==
=={{header|Fortran}}==
In FORTRAN 66 or later, define a function:
<lang fortran>
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>
<lang fortran>
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>
(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:
=={{header|Groovy}}==
Line 260 ⟶ 252:
=={{header|Haskell}}==
<lang
Alternatively,
<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:
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:
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:
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}}==
=={{header|J}}==
Line 311 ⟶ 303:
=={{header|Joy}}==
<lang joy>DEFINE multiply == * .</lang>
▲</lang>
=={{header|Logo}}==
=={{header|Lucid}}==
=={{header|LSE64}}==
=={{header|M4}}==
<lang M4>define(`multiply',`eval($1*$2)')
multiply(2,3)</lang>
▲</lang>
=={{header|Make}}==
Line 338 ⟶ 326:
with recursive make used to retrieve the returned value.
Invoking it
Using gmake, the define syntax is used to define a new function
expr $(1) \* $(2)▼
endef▼
do:▼
@$(call multiply, $(A), $(B))▼
|gmake -f mul.mk do A=5 B=3▼
=={{header|MAXScript}}==
)</lang>▼
=={{header|Metafont}}==
Line 385 ⟶ 373:
=={{header|Modula-3}}==
<lang modula3>PROCEDURE Multiply(a, b: INTEGER): INTEGER =▼
▲PROCEDURE Multiply(a, b: INTEGER): INTEGER =
BEGIN
RETURN a * b;
END Multiply;</lang>
=={{header|Nial}}==
Using variables
Using it
=6</lang>▼
Point free form
Using it
Nial also allows creation of operators
Using it.
=6</lang>▼
Since this is an array programming language, any parameters can be arrays too
=={{header|Oberon-2}}==
Oberon-2 uses procedures, and has a special procedure called a "Function Procedure" used to return a value.
▲ PROCEDURE Multiply(a, b: INTEGER): INTEGER;
BEGIN
RETURN a * b;
END Multiply;</lang>
=={{header|OCaml}}==
Line 440 ⟶ 424:
=={{header|OpenEdge/Progress}}==
=={{header|Pascal}}==
Line 481 ⟶ 465:
=={{header|PL/SQL}}==
=={{header|Pop11}}==
=={{header|PowerShell}}==
Line 542 ⟶ 526:
=={{header|Q}}==
or
or
Using it
<lang
6</lang>
=={{header|R}}==
Line 563 ⟶ 547:
=={{header|Raven}}==
Or optional infix:
Or skip named vars:
=={{header|Ruby}}==
Line 581 ⟶ 565:
=={{header|Scheme}}==
Alternately,
=={{header|Seed7}}==
=={{header|Slate}}==
<lang slate>define: #multiply -> [| :a :b | a * b].</lang>
▲</lang>
or using a macro:
<lang slate>define: #multiple -> #* `er.</lang>
▲</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.
=={{header|Standard ML}}==
<lang
Equivalently,
<lang
Curried form:
<lang
=={{header|Tcl}}==
Line 638 ⟶ 618:
=={{header|TI-89 BASIC}}==
=={{header|Toka}}==
=={{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>
▲</lang>
this is the definition using lambda abstraction
<lang Ursala>multiply = ("a","b"). math..mul ("a","b")</lang>
▲</lang>
and this is the definition using pattern matching.
<lang Ursala>multiply("a","b") = math..mul ("a","b")</lang>
</lang>▼
=={{header|V}}==
Line 671 ⟶ 645:
of actions supplied in the quote.
Using it
▲=6</lang>
V also allows internal bindings.
=={{header|Visual Basic .NET}}==
Call the function
=={{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).
<xsl:call-template name="product"/> <-- using default parameters of 2 and 3 -->
|