Function definition: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 17: Line 17:
=={{header|11l}}==
=={{header|11l}}==
Function definition:
Function definition:
<lang 11l>F multiply(a, b)
<syntaxhighlight lang="11l">F multiply(a, b)
R a * b</lang>
R a * b</syntaxhighlight>
Lambda function definition:
Lambda function definition:
<lang 11l>V multiply = (a, b) -> a * b</lang>
<syntaxhighlight lang="11l">V multiply = (a, b) -> a * b</syntaxhighlight>


=={{header|360 Assembly}}==
=={{header|360 Assembly}}==
Linkage conventions are: register 1 : the parameter list, register 0 : the return value,
Linkage conventions are: register 1 : the parameter list, register 0 : the return value,
and register 14 : the return address.
and register 14 : the return address.
<lang 360asm>DEFFUN CSECT
<syntaxhighlight lang="360asm">DEFFUN CSECT
USING DEFFUN,R13
USING DEFFUN,R13
SAVEAREA B PROLOG-SAVEAREA(R15)
SAVEAREA B PROLOG-SAVEAREA(R15)
Line 62: Line 62:
Z DS F
Z DS F
YREGS
YREGS
END DEFFUN</lang>
END DEFFUN</syntaxhighlight>


=={{header|6502 Assembly}}==
=={{header|6502 Assembly}}==
As with other low-level languages, 6502 assembler has subroutines rather than functions in the strict sense. This implementation of <tt>MULTIPLY</tt> behaves rather like a function, however: it expects two 'parameters' to be passed in the index registers <tt>X</tt> and <tt>Y</tt> and it returns the answer in the accumulator. Note that the 6502 has no <tt>MUL</tt> instruction, so multiplication is carried out by repeated addition.
As with other low-level languages, 6502 assembler has subroutines rather than functions in the strict sense. This implementation of <tt>MULTIPLY</tt> behaves rather like a function, however: it expects two 'parameters' to be passed in the index registers <tt>X</tt> and <tt>Y</tt> and it returns the answer in the accumulator. Note that the 6502 has no <tt>MUL</tt> instruction, so multiplication is carried out by repeated addition.
<lang asm6502>MULTIPLY: STX MULN ; 6502 has no "acc += xreg" instruction,
<syntaxhighlight lang="asm6502">MULTIPLY: STX MULN ; 6502 has no "acc += xreg" instruction,
TXA ; so use a memory address
TXA ; so use a memory address
MULLOOP: DEY
MULLOOP: DEY
Line 73: Line 73:
CPY #$01
CPY #$01
BNE MULLOOP
BNE MULLOOP
RTS</lang>
RTS</syntaxhighlight>
An alternative implementation that multiplies A by X and checks if A/X is zero.
An alternative implementation that multiplies A by X and checks if A/X is zero.
<lang asm6502>; https://skilldrick.github.io/easy6502/
<syntaxhighlight lang="asm6502">; https://skilldrick.github.io/easy6502/
; Multiplies A by X
; Multiplies A by X


Line 96: Line 96:
MAIN: LDA #50
MAIN: LDA #50
LDX #5
LDX #5
JSR MULTIPLY</lang>
JSR MULTIPLY</syntaxhighlight>


=={{header|68000 Assembly}}==
=={{header|68000 Assembly}}==
What values are returned (if any) and where they are returned, will depend on the calling convention used. Code written by a C compiler will typically pass parameters onto the stack and use a "frame pointer" to reference them. For this simple example, the operands will be passed into the function using the registers <code>D0</code> and <code>D1</code>, and the output will be in <code>D0</code>. A function is called by using <code>JSR foo</code> where <code>foo</code> is a labeled section of code or a 24-bit memory address. Execution will continue along starting at that address, until an <code>RTS</code> is encountered, at which point the return address will be popped off the stack into the program counter.
What values are returned (if any) and where they are returned, will depend on the calling convention used. Code written by a C compiler will typically pass parameters onto the stack and use a "frame pointer" to reference them. For this simple example, the operands will be passed into the function using the registers <code>D0</code> and <code>D1</code>, and the output will be in <code>D0</code>. A function is called by using <code>JSR foo</code> where <code>foo</code> is a labeled section of code or a 24-bit memory address. Execution will continue along starting at that address, until an <code>RTS</code> is encountered, at which point the return address will be popped off the stack into the program counter.


<lang 68000devpac>MOVE.L D0,#$0200
<syntaxhighlight lang="68000devpac">MOVE.L D0,#$0200
MOVE.L D1,#$0400
MOVE.L D1,#$0400


Line 113: Line 113:
MULU D0,D1
MULU D0,D1
RTS
RTS
</syntaxhighlight>
</lang>




=={{header|8051 Assembly}}==
=={{header|8051 Assembly}}==
Like other assembly languages, 8051 doesn't have functions but instead has symbolic references to code. Function arguments are passed via registers decided on beforehand.
Like other assembly languages, 8051 doesn't have functions but instead has symbolic references to code. Function arguments are passed via registers decided on beforehand.
<lang asm>ORG RESET
<syntaxhighlight lang="asm">ORG RESET
mov a, #100
mov a, #100
mov b, #10
mov b, #10
Line 129: Line 129:
multiply:
multiply:
mul ab
mul ab
ret</lang>
ret</syntaxhighlight>


=={{header|8086 Assembly}}==
=={{header|8086 Assembly}}==
Line 136: Line 136:
It's important to remember that, unlike other languages, execution of assembly code (and this is true for all assembly languages, not just the 8086) is on a purely linear path by default, much like in other "primitive" languages like BASIC, and so there is nothing stopping the instruction pointer from "falling into" subroutines. Often this can be handy if you're trying to code a variation on a function whose only difference is doing a few extra things at the beginning, but it's something you'll need to guard against, either with a return to the operating system or an infinite loop.
It's important to remember that, unlike other languages, execution of assembly code (and this is true for all assembly languages, not just the 8086) is on a purely linear path by default, much like in other "primitive" languages like BASIC, and so there is nothing stopping the instruction pointer from "falling into" subroutines. Often this can be handy if you're trying to code a variation on a function whose only difference is doing a few extra things at the beginning, but it's something you'll need to guard against, either with a return to the operating system or an infinite loop.


<lang asm>start:
<syntaxhighlight lang="asm">start:
mov al, 0x04
mov al, 0x04
mov bl, 0x05
mov bl, 0x05
Line 146: Line 146:
multiply:
multiply:
mul bl ;outputs 0x0014 to ax
mul bl ;outputs 0x0014 to ax
ret</lang>
ret</syntaxhighlight>


=={{header|AArch64 Assembly}}==
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program functMul64.s */
/* program functMul64.s */
Line 206: Line 206:
/* for this file see task include a file in language AArch64 assembly */
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>


=={{header|ACL2}}==
=={{header|ACL2}}==
<lang Lisp>(defun multiply (a b) (* a b))</lang>
<syntaxhighlight lang="lisp">(defun multiply (a b) (* a b))</syntaxhighlight>


=={{header|ActionScript}}==
=={{header|ActionScript}}==
<lang actionscript>function multiply(a:Number, b:Number):Number {
<syntaxhighlight lang="actionscript">function multiply(a:Number, b:Number):Number {
return a * b;
return a * b;
}</lang>
}</syntaxhighlight>


=={{header|Ada}}==
=={{header|Ada}}==
<lang ada>function Multiply (A, B : Float) return Float;</lang>
<syntaxhighlight lang="ada">function Multiply (A, B : Float) return Float;</syntaxhighlight>
and an implementation of:
and an implementation of:
<lang ada>function Multiply (A, B : Float) return Float is
<syntaxhighlight lang="ada">function Multiply (A, B : Float) return Float is
begin
begin
return A * B;
return A * B;
end Multiply;</lang>
end Multiply;</syntaxhighlight>




The Ada 2012 standard provides an even simpler way to define and implement functions:
The Ada 2012 standard provides an even simpler way to define and implement functions:


<lang Ada>function Multiply(A, B: Float) return Float is (A * B);</lang>
<syntaxhighlight lang="ada">function Multiply(A, B: Float) return Float is (A * B);</syntaxhighlight>




Ada supports generic functions which can take generic formal parameters like the numeric type to use:
Ada supports generic functions which can take generic formal parameters like the numeric type to use:
<lang ada>generic
<syntaxhighlight lang="ada">generic
type Number is digits <>;
type Number is digits <>;
function Multiply (A, B : Number) return Number;</lang>
function Multiply (A, B : Number) return Number;</syntaxhighlight>
implemented as:
implemented as:
<lang ada>function Multiply (A, B : Number) return Number is
<syntaxhighlight lang="ada">function Multiply (A, B : Number) return Number is
begin
begin
return A * B;
return A * B;
end Multiply;</lang>
end Multiply;</syntaxhighlight>
To use this, you need to instantiate the function for each type e.g.
To use this, you need to instantiate the function for each type e.g.
<lang ada>
<syntaxhighlight lang="ada">
with Multiply;
with Multiply;
...
...
Line 248: Line 248:
type My_Integer is Range -100..100;
type My_Integer is Range -100..100;
function Multiply_My_Integer is new Multiply(My_Integer);
function Multiply_My_Integer is new Multiply(My_Integer);
</syntaxhighlight>
</lang>


=={{header|Aime}}==
=={{header|Aime}}==
<lang aime>real
<syntaxhighlight lang="aime">real
multiply(real a, real b)
multiply(real a, real b)
{
{
return a * b;
return a * b;
}</lang>
}</syntaxhighlight>


=={{header|ALGOL 60}}==
=={{header|ALGOL 60}}==
Line 277: Line 277:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<lang algol68>PROC multiply = ( LONG REAL a, b ) LONG REAL:
<syntaxhighlight lang="algol68">PROC multiply = ( LONG REAL a, b ) LONG REAL:
(
(
a * b
a * b
)</lang>
)</syntaxhighlight>


=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
<lang algolw>long real procedure multiply( long real value a, b );
<syntaxhighlight lang="algolw">long real procedure multiply( long real value a, b );
begin
begin
a * b
a * b
end</lang>
end</syntaxhighlight>


=={{header|ALGOL-M}}==
=={{header|ALGOL-M}}==
This implementation takes two integers and returns an integer. Note that a function is distinguished from a procedure, which does not return a value.
This implementation takes two integers and returns an integer. Note that a function is distinguished from a procedure, which does not return a value.
<lang algol>INTEGER FUNCTION MULTIPLY( A, B );
<syntaxhighlight lang="algol">INTEGER FUNCTION MULTIPLY( A, B );
INTEGER A, B;
INTEGER A, B;
BEGIN
BEGIN
MULTIPLY := A * B;
MULTIPLY := A * B;
END;</lang>
END;</syntaxhighlight>


=={{header|AmigaE}}==
=={{header|AmigaE}}==
<lang amigae>PROC my_molt(a,b)
<syntaxhighlight lang="amigae">PROC my_molt(a,b)
-> other statements if needed... here they are not
-> other statements if needed... here they are not
ENDPROC a*b -> return value
ENDPROC a*b -> return value
Line 307: Line 307:
PROC main()
PROC main()
WriteF('\d\n', my_molt(10,20))
WriteF('\d\n', my_molt(10,20))
ENDPROC</lang>
ENDPROC</syntaxhighlight>


=={{header|AntLang}}==
=={{header|AntLang}}==
<lang AntLang>multiply: * /`*' is a normal function
<syntaxhighlight lang="antlang">multiply: * /`*' is a normal function
multiply: {x * y}</lang>
multiply: {x * y}</syntaxhighlight>
Explicit definition has the syntax:
Explicit definition has the syntax:
<lang AntLang>{expr-or-def1; expr-or-def2; ..; return-expr}</lang>
<syntaxhighlight lang="antlang">{expr-or-def1; expr-or-def2; ..; return-expr}</syntaxhighlight>
Inside functions, the variable args contains the sequence of arguments.
Inside functions, the variable args contains the sequence of arguments.
x, y and z contain the first, second and third argument.
x, y and z contain the first, second and third argument.
Line 319: Line 319:
=={{header|APL}}==
=={{header|APL}}==
{{works with|GNU_APL}}
{{works with|GNU_APL}}
<lang apl>
<syntaxhighlight lang="apl">
⍝⍝ APL2 'tradfn' (traditional function)
⍝⍝ APL2 'tradfn' (traditional function)
⍝⍝ This syntax works in all dialects including GNU APL and Dyalog.
⍝⍝ This syntax works in all dialects including GNU APL and Dyalog.
Line 328: Line 328:
⍝⍝ A 'dfn' or 'lambda' (anonymous function)
⍝⍝ A 'dfn' or 'lambda' (anonymous function)
multiply ← {⍺×⍵}
multiply ← {⍺×⍵}
</syntaxhighlight>
</lang>


{{works with|Dyalog_APL}}
{{works with|Dyalog_APL}}
<lang apl>
<syntaxhighlight lang="apl">
⍝⍝ Dyalog dfn (lambda) syntax
⍝⍝ Dyalog dfn (lambda) syntax
multiply ← ×
multiply ← ×
</syntaxhighlight>
</lang>
Works on arrays of any rank (any number of dimensions): atoms, lists, tables, etc.
Works on arrays of any rank (any number of dimensions): atoms, lists, tables, etc.


=={{header|AppleScript}}==
=={{header|AppleScript}}==
<lang AppleScript>to multiply(a as number, b as number)
<syntaxhighlight lang="applescript">to multiply(a as number, b as number)
return a * b
return a * b
end</lang>
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 header line. When the script's compiled, the handler label is automatically appended to the <code>end</code> line too if it wasn't written in.
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 header line. When the script's compiled, the handler label is automatically appended to the <code>end</code> line too if it wasn't written in.


<lang applescript>on multiply(a, b)
<syntaxhighlight lang="applescript">on multiply(a, b)
return a * b
return a * b
end multiply
end multiply


multiply(2, 3)</lang>
multiply(2, 3)</syntaxhighlight>


AppleScript also offers handlers with "labeled" [sic] parameters. These aren't used much now as the limited choice of label enums makes it difficult to choose ones that make sense in English, although it's just about possible here:
AppleScript also offers handlers with "labeled" [sic] parameters. These aren't used much now as the limited choice of label enums makes it difficult to choose ones that make sense in English, although it's just about possible here:


<lang applescript>on multiplication of a by b
<syntaxhighlight lang="applescript">on multiplication of a by b
return a * b
return a * b
end multiplication
end multiplication


multiplication of 2 by 3 -- Or: (multiplication by 3) of 2, or: 2's (multiplication by 3)</lang>
multiplication of 2 by 3 -- Or: (multiplication by 3) of 2, or: 2's (multiplication by 3)</syntaxhighlight>


Labeled parameters don't need to be in the same order in the calls as in the handler definition, but <code>of</code>, if used, is regarded as a direct parameter and requires some parenthesis if it's not given first or if the context isn't entirely clear.
Labeled parameters don't need to be in the same order in the calls as in the handler definition, but <code>of</code>, if used, is regarded as a direct parameter and requires some parenthesis if it's not given first or if the context isn't entirely clear.
Line 362: Line 362:
For the past few years, handlers with "interleaved" parameters have also been possible. They're a development from AppleScriptObjectiveC and coders can specify their own labels provided these aren't reserved words. Calls to these handlers must reference the handlers' "owners", which are usually represented within the same script by the keyword <code>my</code>. The parameter order is the same in the calls as in the handler definitions:
For the past few years, handlers with "interleaved" parameters have also been possible. They're a development from AppleScriptObjectiveC and coders can specify their own labels provided these aren't reserved words. Calls to these handlers must reference the handlers' "owners", which are usually represented within the same script by the keyword <code>my</code>. The parameter order is the same in the calls as in the handler definitions:


<lang applescript>on multiply:a |by|:b -- 'by' is "barred" here because otherwise it's a reserved word.
<syntaxhighlight lang="applescript">on multiply:a |by|:b -- 'by' is "barred" here because otherwise it's a reserved word.
return a * b
return a * b
end multiply:|by|:
end multiply:|by|:


my multiply:2 |by|:3</lang>
my multiply:2 |by|:3</syntaxhighlight>


=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==
Line 373: Line 373:
Function names in Applesoft BASIC can be longer than two characters but only the first two characters are significant. Function names cannot contain any keywords.
Function names in Applesoft BASIC can be longer than two characters but only the first two characters are significant. Function names cannot contain any keywords.


<lang basic>10 DEF FN MULTIPLY(P) = P(P) * P(P+1)
<syntaxhighlight lang="basic">10 DEF FN MULTIPLY(P) = P(P) * P(P+1)
20 P(1) = 611 : P(2) = 78 : PRINT FN MULTIPLY(1)</lang>
20 P(1) = 611 : P(2) = 78 : PRINT FN MULTIPLY(1)</syntaxhighlight>


<lang basic>47658</lang>
<syntaxhighlight lang="basic">47658</syntaxhighlight>


=={{header|Argile}}==
=={{header|Argile}}==
<lang Argile>use std
<syntaxhighlight lang="argile">use std
.: multiply <real a, real b> :. -> real {a * b}</lang>
.: multiply <real a, real b> :. -> real {a * b}</syntaxhighlight>
with a macro and a variable number of parameters:
with a macro and a variable number of parameters:
<lang Argile>use std
<syntaxhighlight lang="argile">use std
=: multiply <real a> [<real b>...] := -> real {Cgen a (@@1 (Cgen " * " b))}</lang>
=: multiply <real a> [<real b>...] := -> real {Cgen a (@@1 (Cgen " * " b))}</syntaxhighlight>


=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* ARM assembly Raspberry PI */
/* program functMul.s */
/* program functMul.s */
Line 525: Line 525:




</syntaxhighlight>
</lang>


=={{header|ArnoldC}}==
=={{header|ArnoldC}}==
<lang arnoldc>LISTEN TO ME VERY CAREFULLY multiply
<syntaxhighlight lang="arnoldc">LISTEN TO ME VERY CAREFULLY multiply
I NEED YOUR CLOTHES YOUR BOOTS AND YOUR MOTORCYCLE a
I NEED YOUR CLOTHES YOUR BOOTS AND YOUR MOTORCYCLE a
I NEED YOUR CLOTHES YOUR BOOTS AND YOUR MOTORCYCLE b
I NEED YOUR CLOTHES YOUR BOOTS AND YOUR MOTORCYCLE b
Line 539: Line 539:
ENOUGH TALK
ENOUGH TALK
I'LL BE BACK product
I'LL BE BACK product
HASTA LA VISTA, BABY</lang>
HASTA LA VISTA, BABY</syntaxhighlight>


=={{header|Arturo}}==
=={{header|Arturo}}==
<lang arturo>multiply: $[x,y][x*y]
<syntaxhighlight lang="arturo">multiply: $[x,y][x*y]


print multiply 3 7
print multiply 3 7
Line 551: Line 551:


print multiply2 3 7
print multiply2 3 7
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 559: Line 559:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang autohotkey>MsgBox % multiply(10,2)
<syntaxhighlight lang="autohotkey">MsgBox % multiply(10,2)


multiply(multiplicand, multiplier) {
multiply(multiplicand, multiplier) {
Return (multiplicand * multiplier)
Return (multiplicand * multiplier)
}</lang>
}</syntaxhighlight>


=={{header|AutoIt}}==
=={{header|AutoIt}}==
<lang AutoIt>#AutoIt Version: 3.2.10.0
<syntaxhighlight lang="autoit">#AutoIt Version: 3.2.10.0
$I=11
$I=11
$J=12
$J=12
Line 572: Line 572:
Func product($a,$b)
Func product($a,$b)
Return $a * $b
Return $a * $b
EndFunc</lang>
EndFunc</syntaxhighlight>


=={{header|AWK}}==
=={{header|AWK}}==
<lang awk>function multiply(a, b)
<syntaxhighlight lang="awk">function multiply(a, b)
{
{
return a*b
return a*b
Line 581: Line 581:
BEGIN {
BEGIN {
print multiply(5, 6)
print multiply(5, 6)
}</lang>
}</syntaxhighlight>


=={{header|Axe}}==
=={{header|Axe}}==
<lang axe>Lbl MULT
<syntaxhighlight lang="axe">Lbl MULT
r₁*r₂
r₁*r₂
Return</lang>
Return</syntaxhighlight>


=={{header|BASIC}}==
=={{header|BASIC}}==
{{works with|QBasic}}
{{works with|QBasic}}
<lang qbasic>DECLARE FUNCTION multiply% (a AS INTEGER, b AS INTEGER)
<syntaxhighlight lang="qbasic">DECLARE FUNCTION multiply% (a AS INTEGER, b AS INTEGER)


FUNCTION multiply% (a AS INTEGER, b AS INTEGER)
FUNCTION multiply% (a AS INTEGER, b AS INTEGER)
multiply = a * b
multiply = a * b
END FUNCTION</lang>
END FUNCTION</syntaxhighlight>


==={{header|BASIC256}}===
==={{header|BASIC256}}===
<lang freebasic>function multiply(a, b)
<syntaxhighlight lang="freebasic">function multiply(a, b)
return a * b
return a * b
end function</lang>
end function</syntaxhighlight>


==={{header|Commodore BASIC}}===
==={{header|Commodore BASIC}}===
In Commodore BASIC function definition can consist of any mathematical operation other functions or commands which result in a numeric expression. The definition is limited to single statement, and it accepts only a single argument. When using the function, keyword fn must precede the function name, which itself must be uniquely distinguishable by its first two characters.
In Commodore BASIC function definition can consist of any mathematical operation other functions or commands which result in a numeric expression. The definition is limited to single statement, and it accepts only a single argument. When using the function, keyword fn must precede the function name, which itself must be uniquely distinguishable by its first two characters.
<lang basic>10 DEF FN MULT(X) = X*Y
<syntaxhighlight lang="basic">10 DEF FN MULT(X) = X*Y
20 Y = 4 : REM VALUE OF SECOND ARGUMENT MUST BE ASSIGNED SEPARATELY
20 Y = 4 : REM VALUE OF SECOND ARGUMENT MUST BE ASSIGNED SEPARATELY
30 PRINT FN MULT(3)</lang>
30 PRINT FN MULT(3)</syntaxhighlight>


==={{header|IS-BASIC}}===
==={{header|IS-BASIC}}===
<lang IS-BASIC>100 DEF MULTIPLY(A,B)=A*B</lang>
<syntaxhighlight lang="is-basic">100 DEF MULTIPLY(A,B)=A*B</syntaxhighlight>


==={{header|GWBASIC}}===
==={{header|GWBASIC}}===
{{works with|BASICA}}
{{works with|BASICA}}
<lang BASIC>10 DEF FNMULT(X,Y)=X*Y
<syntaxhighlight lang="basic">10 DEF FNMULT(X,Y)=X*Y
20 PRINT FNMULT(5,6)
20 PRINT FNMULT(5,6)
39 END
39 END
</syntaxhighlight>
</lang>


==={{header|OxygenBasic}}===
==={{header|OxygenBasic}}===
<lang>
<syntaxhighlight lang="text">
'SHORT FORMS:
'SHORT FORMS:
float multiply(float a,b) = a * b
float multiply(float a,b) = a * b
Line 635: Line 635:
'TEST:
'TEST:
print multiply(pi,2) '6.28...
print multiply(pi,2) '6.28...
</syntaxhighlight>
</lang>


==={{header|QBasic}}===
==={{header|QBasic}}===
<lang qbasic>'This function could either be used for all numeric types
<syntaxhighlight lang="qbasic">'This function could either be used for all numeric types
'(as they are implicitly convertible to Double)
'(as they are implicitly convertible to Double)
FUNCTION multiply# (a AS DOUBLE, b AS DOUBLE)
FUNCTION multiply# (a AS DOUBLE, b AS DOUBLE)
Line 649: Line 649:


PRINT multiply(3, 1.23456)
PRINT multiply(3, 1.23456)
PRINT FNmultiply#(3, 1.23456)</lang>
PRINT FNmultiply#(3, 1.23456)</syntaxhighlight>
{{out}}
{{out}}
<pre> 3.703680038452148</pre>
<pre> 3.703680038452148</pre>
Line 655: Line 655:
==={{header|True BASIC}}===
==={{header|True BASIC}}===
The <code>FUNCTION</code> and <code>DEF</code> commands are synonymous and can be interchanged.
The <code>FUNCTION</code> and <code>DEF</code> commands are synonymous and can be interchanged.
<lang qbasic>FUNCTION multiply(a, b)
<syntaxhighlight lang="qbasic">FUNCTION multiply(a, b)
LET multiply = a * b
LET multiply = a * b
END FUNCTION
END FUNCTION
Line 663: Line 663:
DEF multiply (a, b) = a * b
DEF multiply (a, b) = a * b


END</lang>
END</syntaxhighlight>


==={{header|uBasic/4tH}}===
==={{header|uBasic/4tH}}===
<lang>Print FUNC(_multiply (23, 65))
<syntaxhighlight lang="text">Print FUNC(_multiply (23, 65))
End
End


_multiply Param (2) : Return (a@ * b@)</lang>
_multiply Param (2) : Return (a@ * b@)</syntaxhighlight>


==={{header|Yabasic}}===
==={{header|Yabasic}}===
<lang yabasic>sub multiply(a, b)
<syntaxhighlight lang="yabasic">sub multiply(a, b)
return a * b
return a * b
end sub</lang>
end sub</syntaxhighlight>


=={{header|Batch File}}==
=={{header|Batch File}}==
Windows batch files only have procedures, not functions. Instead, environmental variables can be used as a global shared state.
Windows batch files only have procedures, not functions. Instead, environmental variables can be used as a global shared state.
<lang>@ECHO OFF
<syntaxhighlight lang="text">@ECHO OFF
SET /A result = 0
SET /A result = 0
CALL :multiply 2 3
CALL :multiply 2 3
Line 688: Line 688:
GOTO :eof
GOTO :eof


:eof</lang>
:eof</syntaxhighlight>


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
Line 694: Line 694:


Single-line function:
Single-line function:
<lang bbcbasic>PRINT FNmultiply(6,7)
<syntaxhighlight lang="bbcbasic">PRINT FNmultiply(6,7)
END
END


DEF FNmultiply(a,b) = a * b</lang>
DEF FNmultiply(a,b) = a * b</syntaxhighlight>
Multiline function:
Multiline function:
<lang bbcbasic>DEF FNmultiply(a,b)
<syntaxhighlight lang="bbcbasic">DEF FNmultiply(a,b)
LOCAL c
LOCAL c
c = a * b
c = a * b
= c</lang>
= c</syntaxhighlight>


=={{header|bc}}==
=={{header|bc}}==
{{Works with|GNU bc}}
{{Works with|GNU bc}}
<lang bc>define multiply(a, b) { return a*b }
<syntaxhighlight lang="bc">define multiply(a, b) { return a*b }


print multiply(2, 3)</lang>
print multiply(2, 3)</syntaxhighlight>


=={{header|BCPL}}==
=={{header|BCPL}}==
A function is simply defined as an expression in terms of its arguments.
A function is simply defined as an expression in terms of its arguments.
<lang bcpl>let multiply(a, b) = a * b</lang>
<syntaxhighlight lang="bcpl">let multiply(a, b) = a * b</syntaxhighlight>


Defining a block of code that executes some statements and then returns a
Defining a block of code that executes some statements and then returns a
Line 719: Line 719:
to define a function containing imperative statements. When used this way,
to define a function containing imperative statements. When used this way,
it is equivalent to the functions in most other imperative languages.
it is equivalent to the functions in most other imperative languages.
<lang bcpl>let multiply(a, b) = valof
<syntaxhighlight lang="bcpl">let multiply(a, b) = valof
$( // any imperative statements could go here
$( // any imperative statements could go here
resultis a * b
resultis a * b
$)</lang>
$)</syntaxhighlight>


=={{header|BlitzMax}}==
=={{header|BlitzMax}}==
<lang blitzmax>function multiply:float( a:float, b:float )
<syntaxhighlight lang="blitzmax">function multiply:float( a:float, b:float )
return a*b
return a*b
end function
end function


print multiply(3.1416, 1.6180)</lang>
print multiply(3.1416, 1.6180)</syntaxhighlight>


{{out}}<pre>5.08310890</pre>
{{out}}<pre>5.08310890</pre>


=={{header|Boo}}==
=={{header|Boo}}==
<lang boo>def multiply(x as int, y as int):
<syntaxhighlight lang="boo">def multiply(x as int, y as int):
return x * y
return x * y


print multiply(3, 2)</lang>
print multiply(3, 2)</syntaxhighlight>


=={{header|BQN}}==
=={{header|BQN}}==
Tacit definition:
Tacit definition:
<lang bqn>Multiply ← ×</lang>
<syntaxhighlight lang="bqn">Multiply ← ×</syntaxhighlight>


With names:
With names:
<lang bqn>Multiply ← {𝕨×𝕩}</lang>
<syntaxhighlight lang="bqn">Multiply ← {𝕨×𝕩}</syntaxhighlight>


=={{header|Bracmat}}==
=={{header|Bracmat}}==
<lang bracmat>multiply=a b.!arg:(?a.?b)&!a*!b;
<syntaxhighlight lang="bracmat">multiply=a b.!arg:(?a.?b)&!a*!b;
out$multiply$(123456789.987654321); { writes 121932631112635269 to standard output }</lang>
out$multiply$(123456789.987654321); { writes 121932631112635269 to standard output }</syntaxhighlight>


=={{header|Brat}}==
=={{header|Brat}}==
<lang brat>multiply = { x, y | x * y }
<syntaxhighlight lang="brat">multiply = { x, y | x * y }


p multiply 3 14 #Prints 42</lang>
p multiply 3 14 #Prints 42</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
<lang c>double multiply(double a, double b)
<syntaxhighlight lang="c">double multiply(double a, double b)
{
{
return a * b;
return a * b;
}</lang>
}</syntaxhighlight>
===Macros===
===Macros===
Macros can be defined at the top of a program and the compiler will replace the function calls with the function itself before compiling the program (the source file will not change).
Macros can be defined at the top of a program and the compiler will replace the function calls with the function itself before compiling the program (the source file will not change).
<lang c>#define MULTIPLY(X, Y) ((X) * (Y))</lang>
<syntaxhighlight lang="c">#define MULTIPLY(X, Y) ((X) * (Y))</syntaxhighlight>
Parentheses should be added around parameters in the function definition to avoid order of operations errors when someone uses the macro as such:
Parentheses should be added around parameters in the function definition to avoid order of operations errors when someone uses the macro as such:
<lang c>x = MULTIPLY(x + z, y);</lang>
<syntaxhighlight lang="c">x = MULTIPLY(x + z, y);</syntaxhighlight>
A program with that call would be compiled as if this were coded instead:
A program with that call would be compiled as if this were coded instead:
<lang c>x = ((x + z) * (y));</lang>
<syntaxhighlight lang="c">x = ((x + z) * (y));</syntaxhighlight>
Another advantage of macros is that they work with all types alike. For example, the above macro can be used both to multiply double values (like the function above), and to multiply int values (giving an int, which the function doesn't).
Another advantage of macros is that they work with all types alike. For example, the above macro can be used both to multiply double values (like the function above), and to multiply int values (giving an int, which the function doesn't).


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>static double multiply(double a, double b)
<syntaxhighlight lang="csharp">static double multiply(double a, double b)
{
{
return a * b;
return a * b;
}</lang>
}</syntaxhighlight>
Anonymous function:
Anonymous function:
<lang csharp>Func<double, double, double> multiply = ((a,b) => a*b);</lang>
<syntaxhighlight lang="csharp">Func<double, double, double> multiply = ((a,b) => a*b);</syntaxhighlight>


=={{header|C++}}==
=={{header|C++}}==
Line 781: Line 781:


An inline function differs from the normal function by the keyword inline and the fact that it has to be included in every translation unit which uses it (i.e. it normally is written directly in the header). It allows the compiler to eliminate the function without having the disadvantages of macros (like unintended double evaluation and not respecting scope), because the substitution doesn't happen at source level, but during compilation. An inline version of the above function is:
An inline function differs from the normal function by the keyword inline and the fact that it has to be included in every translation unit which uses it (i.e. it normally is written directly in the header). It allows the compiler to eliminate the function without having the disadvantages of macros (like unintended double evaluation and not respecting scope), because the substitution doesn't happen at source level, but during compilation. An inline version of the above function is:
<lang cpp>inline double multiply(double a, double b)
<syntaxhighlight lang="cpp">inline double multiply(double a, double b)
{
{
return a*b;
return a*b;
}</lang>
}</syntaxhighlight>
If not only doubles, but numbers of arbitrary types are to be multiplied, a function template can be used:
If not only doubles, but numbers of arbitrary types are to be multiplied, a function template can be used:
<lang cpp>template<typename Number>
<syntaxhighlight lang="cpp">template<typename Number>
Number multiply(Number a, Number b)
Number multiply(Number a, Number b)
{
{
return a*b;
return a*b;
}</lang>
}</syntaxhighlight>
Of course, both inline and template may be combined (the <tt>inline</tt> then has to follow the <tt>template&lt;...&gt;</tt>), but since templates have to be in the header anyway (while the standard allows them to be compiled separately using the keyword <tt>export</tt>, almost no compiler implements that), the compiler usually can inline the template even without the keyword.
Of course, both inline and template may be combined (the <tt>inline</tt> then has to follow the <tt>template&lt;...&gt;</tt>), but since templates have to be in the header anyway (while the standard allows them to be compiled separately using the keyword <tt>export</tt>, almost no compiler implements that), the compiler usually can inline the template even without the keyword.


Since C++20, the template parameters can be inferred using <tt>auto</tt>:
Since C++20, the template parameters can be inferred using <tt>auto</tt>:
<lang cpp>auto multiply(auto a, auto b)
<syntaxhighlight lang="cpp">auto multiply(auto a, auto b)
{
{
return a*b;
return a*b;
}</lang>
}</syntaxhighlight>


=={{header|ChucK}}==
=={{header|ChucK}}==
<lang>
<syntaxhighlight lang="text">
fun float multiply (float a, float b)
fun float multiply (float a, float b)
{
{
Line 807: Line 807:
// uncomment next line and change values to test
// uncomment next line and change values to test
//<<< multiply(16,4) >>>;
//<<< multiply(16,4) >>>;
</syntaxhighlight>
</lang>


=={{header|Clay}}==
=={{header|Clay}}==
<lang Clay>multiply(x,y) = x * y;</lang>
<syntaxhighlight lang="clay">multiply(x,y) = x * y;</syntaxhighlight>


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang lisp>(defn multiply [x y]
<syntaxhighlight lang="lisp">(defn multiply [x y]
(* x y))
(* x y))


(multiply 4 5)</lang>
(multiply 4 5)</syntaxhighlight>
Or with multiple arities (in the manner of the actual <tt>*</tt> function):
Or with multiple arities (in the manner of the actual <tt>*</tt> function):
<lang lisp>(defn multiply
<syntaxhighlight lang="lisp">(defn multiply
([] 1)
([] 1)
([x] x)
([x] x)
Line 825: Line 825:
(reduce * (* x y) more)))
(reduce * (* x y) more)))


(multiply 2 3 4 5) ; 120</lang>
(multiply 2 3 4 5) ; 120</syntaxhighlight>


=={{header|CLU}}==
=={{header|CLU}}==
The following is a function that multiplies two integers and ignores any error conditions
The following is a function that multiplies two integers and ignores any error conditions
(as most examples do).
(as most examples do).
<lang clu>multiply = proc (a, b: int) returns (int)
<syntaxhighlight lang="clu">multiply = proc (a, b: int) returns (int)
return(a * b)
return(a * b)
end multiply</lang>
end multiply</syntaxhighlight>


The following is a type-parameterized function that wraps the built-in multiplication operator
The following is a type-parameterized function that wraps the built-in multiplication operator
Line 838: Line 838:
It also shows the complete syntax of a function definition (type parameterization,
It also shows the complete syntax of a function definition (type parameterization,
signals, and a <code>where</code> clause).
signals, and a <code>where</code> clause).
<lang clu>multiply = proc [T: type] (a, b: T) returns (T)
<syntaxhighlight lang="clu">multiply = proc [T: type] (a, b: T) returns (T)
signals (overflow, underflow)
signals (overflow, underflow)
where T has mul: proctype (T, T) returns (T)
where T has mul: proctype (T, T) returns (T)
signals (overflow, underflow)
signals (overflow, underflow)
return(a * b) resignal overflow, underflow
return(a * b) resignal overflow, underflow
end multiply</lang>
end multiply</syntaxhighlight>


=={{header|COBOL}}==
=={{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 program:
In COBOL, ''multiply'' is a reserved word, so the requirements must be relaxed to allow a different function name. The following uses a program:
{{works with|OpenCOBOL}}
{{works with|OpenCOBOL}}
<lang COBOL> IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. myTest.
PROGRAM-ID. myTest.
DATA DIVISION.
DATA DIVISION.
Line 873: Line 873:
MULTIPLY x BY y GIVING z.
MULTIPLY x BY y GIVING z.
EXIT PROGRAM.
EXIT PROGRAM.
END PROGRAM myMultiply.</lang>
END PROGRAM myMultiply.</syntaxhighlight>


This example uses user-defined functions, which were added in COBOL 2002.
This example uses user-defined functions, which were added in COBOL 2002.
{{works with|GNU Cobol|2.0}}
{{works with|GNU Cobol|2.0}}
<lang cobol> IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. myTest.
PROGRAM-ID. myTest.
ENVIRONMENT DIVISION.
ENVIRONMENT DIVISION.
Line 902: Line 902:
MULTIPLY x BY y GIVING z.
MULTIPLY x BY y GIVING z.
EXIT FUNCTION.
EXIT FUNCTION.
END FUNCTION myMultiply.</lang>
END FUNCTION myMultiply.</syntaxhighlight>


=={{header|Coco}}==
=={{header|Coco}}==
Line 908: Line 908:
As CoffeeScript. In addition, Coco provides some syntactic sugar for accessing the <code>arguments</code> array reminiscent of Perl's <code>@_</code>:
As CoffeeScript. In addition, Coco provides some syntactic sugar for accessing the <code>arguments</code> array reminiscent of Perl's <code>@_</code>:


<lang coco>multiply = -> @@0 * @@1</lang>
<syntaxhighlight lang="coco">multiply = -> @@0 * @@1</syntaxhighlight>


Furthermore, when no parameter list is defined, the first argument is available as <code>it</code>:
Furthermore, when no parameter list is defined, the first argument is available as <code>it</code>:


<lang coco>double = -> 2 * it</lang>
<syntaxhighlight lang="coco">double = -> 2 * it</syntaxhighlight>


=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==
<lang coffeescript>multiply = (a, b) -> a * b</lang>
<syntaxhighlight lang="coffeescript">multiply = (a, b) -> a * b</syntaxhighlight>


=={{header|ColdFusion}}==
=={{header|ColdFusion}}==
<lang coldfusion><cffunction name="multiply" returntype="numeric">
<syntaxhighlight lang="coldfusion"><cffunction name="multiply" returntype="numeric">
<cfargument name="a" type="numeric">
<cfargument name="a" type="numeric">
<cfargument name="b" type="numeric">
<cfargument name="b" type="numeric">
<cfreturn a * b>
<cfreturn a * b>
</cffunction></lang>
</cffunction></syntaxhighlight>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Line 928: Line 928:
===Ordinary Functions===
===Ordinary Functions===
Ordinary functions operate on the values of argument expressions. Lisp functions terminate by returning one or more values, or by executing a non-local dynamic control transfer, in which case values are not returned.
Ordinary functions operate on the values of argument expressions. Lisp functions terminate by returning one or more values, or by executing a non-local dynamic control transfer, in which case values are not returned.
<lang lisp>(defun multiply (a b)
<syntaxhighlight lang="lisp">(defun multiply (a b)
(* a b))
(* a b))


(multiply 2 3)</lang>
(multiply 2 3)</syntaxhighlight>
====User-Defined Compiler Optimization of Functions====
====User-Defined Compiler Optimization of Functions====
In Lisp we can express optimizations of calls to a function using compiler macros. For instance, suppose we know that the multiply function, which may be in another module, simply multiplies numbers together. We can replace a call to multiply by a constant, if the arguments are constant expressions. Like the usual kind of Lisp macro, the compiler macro takes the argument forms as arguments, not the argument values. The special keyword &whole gives the macro access to the entire expression, which is convenient for the unhandled cases, whereby no transformation takes place:
In Lisp we can express optimizations of calls to a function using compiler macros. For instance, suppose we know that the multiply function, which may be in another module, simply multiplies numbers together. We can replace a call to multiply by a constant, if the arguments are constant expressions. Like the usual kind of Lisp macro, the compiler macro takes the argument forms as arguments, not the argument values. The special keyword &whole gives the macro access to the entire expression, which is convenient for the unhandled cases, whereby no transformation takes place:
<lang lisp>(define-compiler-macro multiply (&whole expr a b)
<syntaxhighlight lang="lisp">(define-compiler-macro multiply (&whole expr a b)
(if (and (constantp a) (constantp b))
(if (and (constantp a) (constantp b))
(* (eval a) (eval b))
(* (eval a) (eval b))
expr)) ;; no macro recursion if we just return expr; the job is done! </lang>
expr)) ;; no macro recursion if we just return expr; the job is done! </syntaxhighlight>
Lisp implementations do not have to honor compiler macros. Usually compilers make use of them, but evaluators do not.
Lisp implementations do not have to honor compiler macros. Usually compilers make use of them, but evaluators do not.


Line 970: Line 970:


Also, the DEFGENERIC is optional, since the first DEFMETHOD will define the generic function, but good practice.
Also, the DEFGENERIC is optional, since the first DEFMETHOD will define the generic function, but good practice.
<lang lisp>
<syntaxhighlight lang="lisp">
;;; terrific example coming
;;; terrific example coming
</syntaxhighlight>
</lang>


=={{header|Cowgol}}==
=={{header|Cowgol}}==
<lang cowgol>sub multiply(a: int32, b: int32): (rslt: int32) is
<syntaxhighlight lang="cowgol">sub multiply(a: int32, b: int32): (rslt: int32) is
rslt := a * b;
rslt := a * b;
end sub</lang>
end sub</syntaxhighlight>


=={{header|Creative Basic}}==
=={{header|Creative Basic}}==
<syntaxhighlight lang="creative basic">
<lang Creative Basic>
DECLARE Multiply(N1:INT,N2:INT)
DECLARE Multiply(N1:INT,N2:INT)


Line 1,008: Line 1,008:


'Can also be written with no code in the subroutine and just RETURN N1*N2.
'Can also be written with no code in the subroutine and just RETURN N1*N2.
</syntaxhighlight>
</lang>


=={{header|D}}==
=={{header|D}}==
<lang d>// A function:
<syntaxhighlight lang="d">// A function:
int multiply1(int a, int b) {
int multiply1(int a, int b) {
return a * b;
return a * b;
Line 1,034: Line 1,034:
import std.stdio;
import std.stdio;
writeln("2 * 3 = ", result);
writeln("2 * 3 = ", result);
}</lang>
}</syntaxhighlight>
Both the compile-time and run-time output:
Both the compile-time and run-time output:
<pre>6
<pre>6
Line 1,040: Line 1,040:


=={{header|Dart}}==
=={{header|Dart}}==
<lang d>main(){
<syntaxhighlight lang="d">main(){
print(multiply(1,2));
print(multiply(1,2));
print(multiply2(1,2));
print(multiply2(1,2));
Line 1,057: Line 1,057:
return num1 * num2;
return num1 * num2;
}
}
</syntaxhighlight>
</lang>


=={{header|dc}}==
=={{header|dc}}==
For dc, the functions (called macros) are limited to names from 'a' to 'z'
For dc, the functions (called macros) are limited to names from 'a' to 'z'
Create a function called 'm'
Create a function called 'm'
<lang dc>[*] sm</lang>
<syntaxhighlight lang="dc">[*] sm</syntaxhighlight>
Use it (lm loads the function in 'm',x executes it, f shows the the stack.)
Use it (lm loads the function in 'm',x executes it, f shows the the stack.)
<lang dc>3 4 lm x f
<syntaxhighlight lang="dc">3 4 lm x f
= 12</lang>
= 12</syntaxhighlight>


=={{header|Delphi}}==
=={{header|Delphi}}==
In addition to what is shown in the section [[#Pascal|Pascal]], the following is possible too:
In addition to what is shown in the section [[#Pascal|Pascal]], the following is possible too:
<lang delphi>function multiply(a, b: integer): integer;
<syntaxhighlight lang="delphi">function multiply(a, b: integer): integer;
begin
begin
result := a * b;
result := a * b;
end;</lang>
end;</syntaxhighlight>


=={{header|Diego}}==
=={{header|Diego}}==
<lang diego>begin_funct({number}, multiply)_param({number}, a, b);
<syntaxhighlight lang="diego">begin_funct({number}, multiply)_param({number}, a, b);
with_funct[]_calc([a]*[b]);
with_funct[]_calc([a]*[b]);
end_funct[];
end_funct[];


me_msg()_funct(multiply)_param(1,2);</lang>
me_msg()_funct(multiply)_param(1,2);</syntaxhighlight>


=={{header|Draco}}==
=={{header|Draco}}==
Line 1,086: Line 1,086:
return type of the function.
return type of the function.


<lang draco>proc multiply(word a, b) word:
<syntaxhighlight lang="draco">proc multiply(word a, b) word:
a * b
a * b
corp</lang>
corp</syntaxhighlight>


=={{header|Dragon}}==
=={{header|Dragon}}==
<lang dragon>func multiply(a, b) {
<syntaxhighlight lang="dragon">func multiply(a, b) {
return a*b
return a*b
}</lang>
}</syntaxhighlight>


=={{header|DWScript}}==
=={{header|DWScript}}==
<lang Delphi>function Multiply(a, b : Integer) : Integer;
<syntaxhighlight lang="delphi">function Multiply(a, b : Integer) : Integer;
begin
begin
Result := a * b;
Result := a * b;
end;</lang>
end;</syntaxhighlight>


=={{header|Dyalect}}==
=={{header|Dyalect}}==
<lang Dyalect>func multiply(a, b) {
<syntaxhighlight lang="dyalect">func multiply(a, b) {
a * b
a * b
}</lang>
}</syntaxhighlight>


Using lambda syntax:
Using lambda syntax:


<lang Dyalect>let multiply = (a, b) => a * b</lang>
<syntaxhighlight lang="dyalect">let multiply = (a, b) => a * b</syntaxhighlight>


=={{header|Déjà Vu}}==
=={{header|Déjà Vu}}==
<lang dejavu>multiply a b:
<syntaxhighlight lang="dejavu">multiply a b:
* a b</lang>
* a b</syntaxhighlight>


=={{header|E}}==
=={{header|E}}==
<lang e>def multiply(a, b) {
<syntaxhighlight lang="e">def multiply(a, b) {
return a * b
return a * b
}</lang>
}</syntaxhighlight>
(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.)
(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.)


It is also possible to write short anonymous function definitions which do not need explicit returns:
It is also possible to write short anonymous function definitions which do not need explicit returns:
<lang e>def multiply := fn a, b { a * b }</lang>
<syntaxhighlight lang="e">def multiply := fn a, b { a * b }</syntaxhighlight>
This definition is identical to the previous except that the function object will not know its own name.
This definition is identical to the previous except that the function object will not know its own name.


=={{header|EasyLang}}==
=={{header|EasyLang}}==
<lang>func multiply a b . r .
<syntaxhighlight lang="text">func multiply a b . r .
r = a * b
r = a * b
.
.
call multiply 7 5 res
call multiply 7 5 res
print res</lang>
print res</syntaxhighlight>


=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
<lang lisp>
<syntaxhighlight lang="lisp">
(define (multiply a b) (* a b)) → multiply ;; (1)
(define (multiply a b) (* a b)) → multiply ;; (1)
(multiply 1/3 666) → 222
(multiply 1/3 666) → 222
Line 1,158: Line 1,158:


multiply → (λ (_a _b) (#🔶_multiply)) ;; compiled function
multiply → (λ (_a _b) (#🔶_multiply)) ;; compiled function
</syntaxhighlight>
</lang>


=={{header|Efene}}==
=={{header|Efene}}==
<lang efene>multiply = fn (A, B) {
<syntaxhighlight lang="efene">multiply = fn (A, B) {
A * B
A * B
}
}
Line 1,168: Line 1,168:
run = fn () {
run = fn () {
io.format("~p~n", [multiply(2, 5)])
io.format("~p~n", [multiply(2, 5)])
}</lang>
}</syntaxhighlight>


=={{header|Eiffel}}==
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
multiply(a, b: INTEGER): INTEGER
multiply(a, b: INTEGER): INTEGER
do
do
Result := a*b
Result := a*b
end
end
</syntaxhighlight>
</lang>


=={{header|Ela}}==
=={{header|Ela}}==
<lang Ela>multiply x y = x * y</lang>
<syntaxhighlight lang="ela">multiply x y = x * y</syntaxhighlight>
Anonymous function:
Anonymous function:
<lang Ela>\x y -> x * y</lang>
<syntaxhighlight lang="ela">\x y -> x * y</syntaxhighlight>


=={{header|Elena}}==
=={{header|Elena}}==
<lang elena>real multiply(real a, real b)
<syntaxhighlight lang="elena">real multiply(real a, real b)
= a * b;</lang>
= a * b;</syntaxhighlight>
Anonymous function / closure:
Anonymous function / closure:
<lang elena>symbol f := (x,y => x * y);</lang>
<syntaxhighlight lang="elena">symbol f := (x,y => x * y);</syntaxhighlight>
Root closure:
Root closure:
<lang elena>f(x,y){ ^ x * y }</lang>
<syntaxhighlight lang="elena">f(x,y){ ^ x * y }</syntaxhighlight>


=={{header|Elixir}}==
=={{header|Elixir}}==
<lang elixir>defmodule RosettaCode do
<syntaxhighlight lang="elixir">defmodule RosettaCode do
def multiply(x,y) do
def multiply(x,y) do
x * y
x * y
Line 1,200: Line 1,200:
end
end


RosettaCode.task</lang>
RosettaCode.task</syntaxhighlight>


{{out}}
{{out}}
Line 1,208: Line 1,208:


=={{header|Elm}}==
=={{header|Elm}}==
<syntaxhighlight lang="elm">
<lang Elm>
--There are multiple ways to create a function in Elm
--There are multiple ways to create a function in Elm


Line 1,216: Line 1,216:
--This is an anonymous function
--This is an anonymous function
\x y -> x*y
\x y -> x*y
</syntaxhighlight>
</lang>


=={{header|Emacs Lisp}}==
=={{header|Emacs Lisp}}==
<lang Lisp>(defun multiply (x y)
<syntaxhighlight lang="lisp">(defun multiply (x y)
(* x y))</lang>
(* x y))</syntaxhighlight>


A "docstring" can be added as follows. This is shown by the Emacs help system and is good for human users. It has no effect on execution.
A "docstring" can be added as follows. This is shown by the Emacs help system and is good for human users. It has no effect on execution.


<lang Lisp>(defun multiply (x y)
<syntaxhighlight lang="lisp">(defun multiply (x y)
"Return the product of X and Y."
"Return the product of X and Y."
(* x y))</lang>
(* x y))</syntaxhighlight>


=={{header|Erlang}}==
=={{header|Erlang}}==
===Using case, multiple lines===
===Using case, multiple lines===
<lang erlang>% Implemented by Arjun Sunel
<syntaxhighlight lang="erlang">% Implemented by Arjun Sunel
-module(func_definition).
-module(func_definition).
-export([main/0]).
-export([main/0]).
Line 1,241: Line 1,241:
case {A,B} of
case {A,B} of
{A, B} -> A * B
{A, B} -> A * B
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>12
<pre>12
Line 1,247: Line 1,247:
</pre>
</pre>
===In a single line===
===In a single line===
<lang erlang>
<syntaxhighlight lang="erlang">
-module(func_definition).
-module(func_definition).
-export([main/0]).
-export([main/0]).
Line 1,255: Line 1,255:
io :format("~p~n",[K]).
io :format("~p~n",[K]).
multiply(A,B) -> A * B.</lang>
multiply(A,B) -> A * B.</syntaxhighlight>
The output is the same.
The output is the same.


Line 1,289: Line 1,289:


=={{header|Euphoria}}==
=={{header|Euphoria}}==
<lang Euphoria>function multiply( atom a, atom b )
<syntaxhighlight lang="euphoria">function multiply( atom a, atom b )
return a * b
return a * b
end function</lang>
end function</syntaxhighlight>
If you declare the arguments as <code>object</code> then sequence comprehension kicks in:
If you declare the arguments as <code>object</code> then sequence comprehension kicks in:
<lang Euphoria>function multiply( object a, object b )
<syntaxhighlight lang="euphoria">function multiply( object a, object b )
return a * b
return a * b
end function
end function
Line 1,304: Line 1,304:
? multiply( a, b )
? multiply( a, b )
? multiply( a, 7 )
? multiply( a, 7 )
? multiply( 10.39564, b )</lang>
? multiply( 10.39564, b )</syntaxhighlight>
{{out}}
{{out}}
<pre>81
<pre>81
Line 1,314: Line 1,314:
=={{header|F Sharp|F#}}==
=={{header|F Sharp|F#}}==
The default will be an integer function but you can specify other types as shown:
The default will be an integer function but you can specify other types as shown:
<lang fsharp>let multiply x y = x * y // integer
<syntaxhighlight lang="fsharp">let multiply x y = x * y // integer
let fmultiply (x : float) (y : float) = x * y</lang>
let fmultiply (x : float) (y : float) = x * y</syntaxhighlight>


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>: multiply ( a b -- a*b ) * ;</lang>
<syntaxhighlight lang="factor">: multiply ( a b -- a*b ) * ;</syntaxhighlight>


=={{header|Falcon}}==
=={{header|Falcon}}==
<lang falcon>function sayHiTo( name )
<syntaxhighlight lang="falcon">function sayHiTo( name )
> "Hi ", name
> "Hi ", name
end</lang>
end</syntaxhighlight>


=={{header|FALSE}}==
=={{header|FALSE}}==
<lang false>[*] {anonymous function to multiply the top two items on the stack}
<syntaxhighlight 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}
m: {binding the function to one of the 26 available symbol names}
2 3m;! {executing the function, yielding 6}</lang>
2 3m;! {executing the function, yielding 6}</syntaxhighlight>


=={{header|Fantom}}==
=={{header|Fantom}}==
<lang fantom>class FunctionDefinition
<syntaxhighlight lang="fantom">class FunctionDefinition
{
{
public static Void main ()
public static Void main ()
Line 1,338: Line 1,338:
echo ("Multiply 2 and 4: ${multiply(2, 4)}")
echo ("Multiply 2 and 4: ${multiply(2, 4)}")
}
}
}</lang>
}</syntaxhighlight>


=={{header|Fermat}}==
=={{header|Fermat}}==
<lang fermat>Func Multiply(a, b) = a*b.</lang>
<syntaxhighlight lang="fermat">Func Multiply(a, b) = a*b.</syntaxhighlight>


=={{header|Fexl}}==
=={{header|Fexl}}==
<lang fexl>\multiply=(\x\y * x y)</lang>
<syntaxhighlight lang="fexl">\multiply=(\x\y * x y)</syntaxhighlight>
Or if I'm being cheeky:
Or if I'm being cheeky:
<lang fexl>\multiply=*</lang>
<syntaxhighlight lang="fexl">\multiply=*</syntaxhighlight>


=={{header|Fish}}==
=={{header|Fish}}==
Functions cannot be named in Fish. However, they can be defined as new stacks that pull a certain number of arguments off the stack that came before. <code>2[</code> says pull 2 values off the stack and put them in a new, separate stack. <code>]</code> says put all remaining values in the current stack onto the top of the stack below (the old stack).
Functions cannot be named in Fish. However, they can be defined as new stacks that pull a certain number of arguments off the stack that came before. <code>2[</code> says pull 2 values off the stack and put them in a new, separate stack. <code>]</code> says put all remaining values in the current stack onto the top of the stack below (the old stack).
<lang fish>2[*]</lang>
<syntaxhighlight lang="fish">2[*]</syntaxhighlight>


=={{header|Forth}}==
=={{header|Forth}}==
<lang forth>: fmultiply ( F: a b -- F: c ) F* ;
<syntaxhighlight lang="forth">: fmultiply ( F: a b -- F: c ) F* ;
: multiply ( a b -- c ) * ;</lang>
: multiply ( a b -- c ) * ;</syntaxhighlight>


=={{header|Fortran}}==
=={{header|Fortran}}==
In FORTRAN I (1957), inline function could be defined at the beginning of the program. Let's note than to specify a floating point real the name of the statement function begins with an X (no type declaration) and to specify this is a function the name ends with a F.
In FORTRAN I (1957), inline function could be defined at the beginning of the program. Let's note than to specify a floating point real the name of the statement function begins with an X (no type declaration) and to specify this is a function the name ends with a F.
<lang fortran> XMULTF(X,Y)=X*Y</lang>
<syntaxhighlight lang="fortran"> XMULTF(X,Y)=X*Y</syntaxhighlight>
And for interger multiplication:
And for interger multiplication:
<lang fortran> MULTF(I,J)=I*J</lang>
<syntaxhighlight lang="fortran"> MULTF(I,J)=I*J</syntaxhighlight>


In FORTRAN IV, FORTRAN 66 or later, define a function:
In FORTRAN IV, FORTRAN 66 or later, define a function:
<lang fortran>FUNCTION MULTIPLY(X,Y)
<syntaxhighlight lang="fortran">FUNCTION MULTIPLY(X,Y)
REAL MULTIPLY, X, Y
REAL MULTIPLY, X, Y
MULTIPLY = X * Y
MULTIPLY = X * Y
END</lang>
END</syntaxhighlight>
And for integer multiplication:
And for integer multiplication:
<lang fortran>FUNCTION MULTINT(X,Y)
<syntaxhighlight lang="fortran">FUNCTION MULTINT(X,Y)
INTEGER MULTINT, X, Y
INTEGER MULTINT, X, Y
MULTINT = X * Y
MULTINT = X * Y
END</lang>
END</syntaxhighlight>


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:
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
<syntaxhighlight lang="fortran">module elemFunc
contains
contains
elemental function multiply(x, y)
elemental function multiply(x, y)
Line 1,381: Line 1,381:
multiply = x * y
multiply = x * y
end function multiply
end function multiply
end module elemFunc</lang>
end module elemFunc</syntaxhighlight>
<lang fortran>program funcDemo
<syntaxhighlight lang="fortran">program funcDemo
use elemFunc
use elemFunc
Line 1,391: Line 1,391:
z = multiply(x,y) ! element-wise invocation only works with elemental function
z = multiply(x,y) ! element-wise invocation only works with elemental function
end program funcDemo</lang>
end program funcDemo</syntaxhighlight>
It is worth noting that Fortran can call functions (and subroutines) using named arguments; e.g. we can call multiply in the following way:
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)
<syntaxhighlight 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>
z = multiply(y=x, x=y) ! the same as multiply(y, x)</syntaxhighlight>
(Because of commutativity property of the multiplication, the difference between <code>multiply(x,y)</code> and <code>multiply(y,x)</code> is not evident)
(Because of commutativity property of the multiplication, the difference between <code>multiply(x,y)</code> and <code>multiply(y,x)</code> is not evident)


Also note that the function result can be declared with a different name within the routine:
Also note that the function result can be declared with a different name within the routine:
<lang fortran>module elemFunc
<syntaxhighlight lang="fortran">module elemFunc
contains
contains
elemental function multiply(x, y) result(z)
elemental function multiply(x, y) result(z)
Line 1,405: Line 1,405:
z = x * y
z = x * y
end function multiply
end function multiply
end module elemFunc</lang>
end module elemFunc</syntaxhighlight>


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64


Function multiply(d1 As Double, d2 As Double) As Double
Function multiply(d1 As Double, d2 As Double) As Double
Return d1 * d2
Return d1 * d2
End Function</lang>
End Function</syntaxhighlight>
This function could either be used for all numeric types (as they are implicitly convertible to Double)
This function could either be used for all numeric types (as they are implicitly convertible to Double)
or could be overloaded to deal with each such type (there are 12 of them).
or could be overloaded to deal with each such type (there are 12 of them).
Line 1,418: Line 1,418:
Alternatively, one could write a macro though this wouldn't be type-safe:
Alternatively, one could write a macro though this wouldn't be type-safe:


<lang freebasic>#Define multiply(d1, d2) (d1) * (d2)</lang>
<syntaxhighlight lang="freebasic">#Define multiply(d1, d2) (d1) * (d2)</syntaxhighlight>


=={{header|Free Pascal}}==
=={{header|Free Pascal}}==
Line 1,427: Line 1,427:
Furthermore, after the assignment to the return variable further statements may follow.
Furthermore, after the assignment to the return variable further statements may follow.
To ensure a value is returned immediately and no further following statements are processed, using the built-in <tt>exit</tt> procedure is possible too in <tt>{$mode objFPC}</tt>:
To ensure a value is returned immediately and no further following statements are processed, using the built-in <tt>exit</tt> procedure is possible too in <tt>{$mode objFPC}</tt>:
<lang delphi>function multiply(a, b: integer): integer;
<syntaxhighlight lang="delphi">function multiply(a, b: integer): integer;
begin
begin
exit(a * b);
exit(a * b);
end;</lang>
end;</syntaxhighlight>
If <tt>exit</tt> has been redefined in the current scope, its special meaning can be accessed via the fully-qualified identifier <tt>system.exit</tt>.
If <tt>exit</tt> has been redefined in the current scope, its special meaning can be accessed via the fully-qualified identifier <tt>system.exit</tt>.
Note, any enclosing <tt>finally</tt> frames of <tt>try … finally … end</tt> are processed first before actually returning from the <tt>function</tt>.
Note, any enclosing <tt>finally</tt> frames of <tt>try … finally … end</tt> are processed first before actually returning from the <tt>function</tt>.
Line 1,437: Line 1,437:
=={{header|Frink}}==
=={{header|Frink}}==
This function works correctly with any combination of arbitrarily-large integers, arbitrary-precision floating point numbers, arbitrary-size rational numbers, complex numbers, intervals of real numbers, and even numbers with units of measure (e.g. <code>multiply[1 watt, 1 s]</code> gives an answer with dimensions of energy. Frink tries hard to always Do The Right Thing with math and numerics and units of measure.
This function works correctly with any combination of arbitrarily-large integers, arbitrary-precision floating point numbers, arbitrary-size rational numbers, complex numbers, intervals of real numbers, and even numbers with units of measure (e.g. <code>multiply[1 watt, 1 s]</code> gives an answer with dimensions of energy. Frink tries hard to always Do The Right Thing with math and numerics and units of measure.
<lang frink>multiply[x,y] := x*y</lang>
<syntaxhighlight lang="frink">multiply[x,y] := x*y</syntaxhighlight>


=={{header|Futhark}}==
=={{header|Futhark}}==


<syntaxhighlight lang="futhark">
<lang Futhark>
let multiply (x: i32, y: i32) : i32 = x * y
let multiply (x: i32, y: i32) : i32 = x * y
</syntaxhighlight>
</lang>


=={{header|FutureBasic}}==
=={{header|FutureBasic}}==
<lang futurebasic>window 1
<syntaxhighlight lang="futurebasic">window 1


local fn multiply( a as long, b as long ) as long
local fn multiply( a as long, b as long ) as long
Line 1,453: Line 1,453:
print fn multiply( 3, 9 )
print fn multiply( 3, 9 )


HandleEvents</lang>
HandleEvents</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 1,461: Line 1,461:
=={{header|Gambas}}==
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=bc93236474d9937217dd4117026f7441 Click this link to run this code]'''
'''[https://gambas-playground.proko.eu/?gist=bc93236474d9937217dd4117026f7441 Click this link to run this code]'''
<lang gambas>Public Sub Main()
<syntaxhighlight lang="gambas">Public Sub Main()


Print Multiply(56, 4.66)
Print Multiply(56, 4.66)
Line 1,471: Line 1,471:
Return f1 * f2
Return f1 * f2


End</lang>
End</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 1,478: Line 1,478:


=={{header|GAP}}==
=={{header|GAP}}==
<lang gap>multiply := function(a, b)
<syntaxhighlight lang="gap">multiply := function(a, b)
return a*b;
return a*b;
end;</lang>
end;</syntaxhighlight>


=={{header|GML}}==
=={{header|GML}}==
In GML one can not define a function but in [[Game Maker]] there is a ''script'' resource, which is the equivalent of a function as defined here. Scripts can be exported to or imported from a text file with the following format:
In GML one can not define a function but in [[Game Maker]] there is a ''script'' resource, which is the equivalent of a function as defined here. Scripts can be exported to or imported from a text file with the following format:
<lang GML>#define multiply
<syntaxhighlight lang="gml">#define multiply
a = argument0
a = argument0
b = argument1
b = argument1
return(a * b)</lang>
return(a * b)</syntaxhighlight>


=={{header|Gnuplot}}==
=={{header|Gnuplot}}==
<lang Gnuplot>multiply(x,y) = x*y
<syntaxhighlight lang="gnuplot">multiply(x,y) = x*y


# then for example
# then for example
print multiply(123,456)</lang>
print multiply(123,456)</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
Line 1,499: Line 1,499:


The return statement can contain an expression of the function return type:
The return statement can contain an expression of the function return type:
<lang go>func multiply(a, b float64) float64 {
<syntaxhighlight lang="go">func multiply(a, b float64) float64 {
return a * b
return a * b
}</lang>
}</syntaxhighlight>
Alternatively, if the return value is named, the return statement does not require an expression:
Alternatively, if the return value is named, the return statement does not require an expression:
<lang go>func multiply(a, b float64) (z float64) {
<syntaxhighlight lang="go">func multiply(a, b float64) (z float64) {
z = a * b
z = a * b
return
return
}</lang>
}</syntaxhighlight>


=={{header|Golfscript}}==
=={{header|Golfscript}}==
<lang golfscript>{*}:multiply;</lang>
<syntaxhighlight lang="golfscript">{*}:multiply;</syntaxhighlight>


=={{header|Groovy}}==
=={{header|Groovy}}==
<lang groovy>def multiply = { x, y -> x * y }</lang>
<syntaxhighlight lang="groovy">def multiply = { x, y -> x * y }</syntaxhighlight>
Test Program:
Test Program:
<lang groovy>println "x * y = 20 * 50 = ${multiply 20, 50}"</lang>
<syntaxhighlight lang="groovy">println "x * y = 20 * 50 = ${multiply 20, 50}"</syntaxhighlight>
{{out}}
{{out}}
<pre>x * y = 20 * 50 = 1000</pre>
<pre>x * y = 20 * 50 = 1000</pre>


=={{header|Halon}}==
=={{header|Halon}}==
<lang halon>function multiply( $a, $b )
<syntaxhighlight lang="halon">function multiply( $a, $b )
{
{
return $a * $b;
return $a * $b;
}</lang>
}</syntaxhighlight>


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>multiply x y = x * y</lang>
<syntaxhighlight lang="haskell">multiply x y = x * y</syntaxhighlight>
Alternatively, with help of auto-currying,
Alternatively, with help of auto-currying,
<lang haskell>multiply = (*)</lang>
<syntaxhighlight lang="haskell">multiply = (*)</syntaxhighlight>
You can use [[lambda-function]]
You can use [[lambda-function]]
<lang haskell>multiply = \ x y -> x*y</lang>
<syntaxhighlight lang="haskell">multiply = \ x y -> x*y</syntaxhighlight>


=={{header|Haxe}}==
=={{header|Haxe}}==
<lang haxe>function multiply(x:Float, y:Float):Float{
<syntaxhighlight lang="haxe">function multiply(x:Float, y:Float):Float{
return x * y;
return x * y;
}</lang>
}</syntaxhighlight>


=={{header|hexiscript}}==
=={{header|hexiscript}}==
<lang hexiscript>fun multiply a b
<syntaxhighlight lang="hexiscript">fun multiply a b
return a * b
return a * b
endfun</lang>
endfun</syntaxhighlight>


=={{header|HicEst}}==
=={{header|HicEst}}==
<lang hicest>FUNCTION multiply(a, b)
<syntaxhighlight lang="hicest">FUNCTION multiply(a, b)
multiply = a * b
multiply = a * b
END</lang>
END</syntaxhighlight>


=={{header|HolyC}}==
=={{header|HolyC}}==
<lang holyc>F64 Multiply(F64 a, F64 b) {
<syntaxhighlight lang="holyc">F64 Multiply(F64 a, F64 b) {
return a * b;
return a * b;
}
}
Line 1,553: Line 1,553:
F64 x;
F64 x;
x = Multiply(42, 13.37);
x = Multiply(42, 13.37);
Print("%5.2f\n", x);</lang>
Print("%5.2f\n", x);</syntaxhighlight>


=={{header|Hy}}==
=={{header|Hy}}==
Function definition:
Function definition:
<lang clojure>(defn multiply [a b]
<syntaxhighlight lang="clojure">(defn multiply [a b]
(* a b))</lang>
(* a b))</syntaxhighlight>
Lambda definition:
Lambda definition:
<lang clojure>(def multiply (fn [a b] (* a b)))</lang>
<syntaxhighlight lang="clojure">(def multiply (fn [a b] (* a b)))</syntaxhighlight>


=={{header|i}}==
=={{header|i}}==
<syntaxhighlight lang="i">
<lang i>
concept multiply(a, b) {
concept multiply(a, b) {
return a*b
return a*b
}
}
</syntaxhighlight>
</lang>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
<lang Icon>procedure multiply(a,b)
<syntaxhighlight lang="icon">procedure multiply(a,b)
return a * b
return a * b
end</lang>
end</syntaxhighlight>


=={{header|IDL}}==
=={{header|IDL}}==
The task description is unclear on what to do when the arguments to the function are non-scalar, so here's multiple versions:
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
<syntaxhighlight lang="idl">function multiply ,a,b
return, a* b
return, a* b
end</lang>
end</syntaxhighlight>
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".
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".


Alternatively, there's this possibility:
Alternatively, there's this possibility:
<lang idl>function multiply ,a,b
<syntaxhighlight lang="idl">function multiply ,a,b
return, product([a, b])
return, product([a, b])
end</lang>
end</syntaxhighlight>
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.
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.


Finally, there's this option:
Finally, there's this option:
<lang idl>function multiply ,a,b
<syntaxhighlight lang="idl">function multiply ,a,b
return, a # b
return, a # b
end</lang>
end</syntaxhighlight>
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.
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|Inform 6}}==
=={{header|Inform 6}}==
<lang inform6>[ multiply a b;
<syntaxhighlight lang="inform6">[ multiply a b;
return a * b;
return a * b;
];</lang>
];</syntaxhighlight>


=={{header|Inform 7}}==
=={{header|Inform 7}}==
<lang inform7>To decide which number is (A - number) multiplied by (B - number):
<syntaxhighlight lang="inform7">To decide which number is (A - number) multiplied by (B - number):
decide on A * B.</lang>
decide on A * B.</syntaxhighlight>


=={{header|Io}}==
=={{header|Io}}==
<lang io>multiply := method(a,b,a*b)</lang>
<syntaxhighlight lang="io">multiply := method(a,b,a*b)</syntaxhighlight>


=={{header|IWBASIC}}==
=={{header|IWBASIC}}==
<syntaxhighlight lang="iwbasic">
<lang IWBASIC>
'1. Not Object Oriented Program
'1. Not Object Oriented Program


Line 1,704: Line 1,704:
ENDSUB
ENDSUB


</syntaxhighlight>
</lang>


=={{header|J}}==
=={{header|J}}==
<lang j>multiply=: *</lang>
<syntaxhighlight lang="j">multiply=: *</syntaxhighlight>
Works on conforming arrays of any rank (any number of dimensions, as long as the dimensions of one are a prefix of the dimensions of the other): atoms, lists, tables, etc.
Works on conforming arrays of any rank (any number of dimensions, as long as the dimensions of one are a prefix of the dimensions of the other): atoms, lists, tables, etc.


Or, more verbosely (and a bit slower, though the speed difference should be unnoticeable in most contexts):
Or, more verbosely (and a bit slower, though the speed difference should be unnoticeable in most contexts):
<lang J>multiply=: dyad define
<syntaxhighlight lang="j">multiply=: dyad define
x * y
x * y
)</lang>
)</syntaxhighlight>
Here we use an [http://www.jsoftware.com/help/dictionary/intro18.htm explicit] definition (where the arguments are named) rather than a [http://www.jsoftware.com/help/dictionary/intro19.htm tacit] version (where the arguments are implied). In explicit J verbs, x is the left argument and y is the right argument.
Here we use an [http://www.jsoftware.com/help/dictionary/intro18.htm explicit] definition (where the arguments are named) rather than a [http://www.jsoftware.com/help/dictionary/intro19.htm tacit] version (where the arguments are implied). In explicit J verbs, x is the left argument and y is the right argument.


Line 1,720: Line 1,720:
=={{header|Java}}==
=={{header|Java}}==
There are no global functions in Java. The equivalent is to define static methods in a class (here invoked as "Math.multiply(a,b)"). Overloading allows us to define the method for multiple types.
There are no global functions in Java. The equivalent is to define static methods in a class (here invoked as "Math.multiply(a,b)"). Overloading allows us to define the method for multiple types.
<lang java>public class Math
<syntaxhighlight lang="java">public class Math
{
{
public static int multiply( int a, int b) { return a*b; }
public static int multiply( int a, int b) { return a*b; }
public static double multiply(double a, double b) { return a*b; }
public static double multiply(double a, double b) { return a*b; }
}</lang>
}</syntaxhighlight>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
===ES1-*===
===ES1-*===
Function Declaration
Function Declaration
<lang javascript>function multiply(a, b) {
<syntaxhighlight lang="javascript">function multiply(a, b) {
return a*b;
return a*b;
}</lang>
}</syntaxhighlight>


===ES3-*===
===ES3-*===
Function Expression
Function Expression
<lang javascript>var multiply = function(a, b) {
<syntaxhighlight lang="javascript">var multiply = function(a, b) {
return a * b;
return a * b;
};</lang>
};</syntaxhighlight>


Named Function Expression
Named Function Expression
<lang javascript>var multiply = function multiply(a, b) {
<syntaxhighlight lang="javascript">var multiply = function multiply(a, b) {
return a * b;
return a * b;
};</lang>
};</syntaxhighlight>


Method Definition
Method Definition
<lang javascript>var o = {
<syntaxhighlight lang="javascript">var o = {
multiply: function(a, b) {
multiply: function(a, b) {
return a * b;
return a * b;
}
}
};</lang>
};</syntaxhighlight>


===ES5-*===
===ES5-*===
Accessors
Accessors
<lang javascript>var o = {
<syntaxhighlight lang="javascript">var o = {
get foo() {
get foo() {
return 1;
return 1;
Line 1,760: Line 1,760:
// do things with value
// do things with value
}
}
};</lang>
};</syntaxhighlight>




===ES6-*===
===ES6-*===
Arrow Function
Arrow Function
<lang javascript>var multiply = (a, b) => a * b;
<syntaxhighlight lang="javascript">var multiply = (a, b) => a * b;
var multiply = (a, b) => { return a * b };
var multiply = (a, b) => { return a * b };
</syntaxhighlight>
</lang>


Concise Body Method Definition
Concise Body Method Definition
<lang javascript>var o = {
<syntaxhighlight lang="javascript">var o = {
multiply(a, b) {
multiply(a, b) {
return a * b;
return a * b;
}
}
};</lang>
};</syntaxhighlight>


Generator Functions
Generator Functions
<lang javascript>function * generator() {
<syntaxhighlight lang="javascript">function * generator() {
yield 1;
yield 1;
}</lang>
}</syntaxhighlight>


=={{header|Joy}}==
=={{header|Joy}}==
<syntaxhighlight lang=Joy>DEFINE multiply == * .</syntaxhighlight>
<syntaxhighlight lang="joy">DEFINE multiply == * .</syntaxhighlight>


=={{header|jq}}==
=={{header|jq}}==
Example of a simple function definition:<lang jq>def multiply(a; b): a*b;</lang>
Example of a simple function definition:<syntaxhighlight lang="jq">def multiply(a; b): a*b;</syntaxhighlight>
Example of the definition of an inner function:<lang jq># 2 | generate(. * .) will generate 2, 4, 16, 256, ...
Example of the definition of an inner function:<syntaxhighlight lang="jq"># 2 | generate(. * .) will generate 2, 4, 16, 256, ...
def generate(f): def r: ., (f | r); r;</lang>
def generate(f): def r: ., (f | r); r;</syntaxhighlight>
The previous example (generate/1) also illustrates that a function argument can be a function or composition of functions. Here is another example:<lang jq>def summation(f): reduce .[] as $x (0; . + ($x|f));</lang>
The previous example (generate/1) also illustrates that a function argument can be a function or composition of functions. Here is another example:<syntaxhighlight lang="jq">def summation(f): reduce .[] as $x (0; . + ($x|f));</syntaxhighlight>
<tt>summation/1</tt> expects an array as its input and takes a function, f, as its argument. For example, if the input array consists of JSON objects with attributes "h" and "w", then to compute SIGMA (h * w) we could simply write:<lang jq>summation( .h * .w)</lang>
<tt>summation/1</tt> expects an array as its input and takes a function, f, as its argument. For example, if the input array consists of JSON objects with attributes "h" and "w", then to compute SIGMA (h * w) we could simply write:<syntaxhighlight lang="jq">summation( .h * .w)</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
Line 1,795: Line 1,795:
General function definition:
General function definition:


<lang julia>function multiply(a::Number, b::Number)
<syntaxhighlight lang="julia">function multiply(a::Number, b::Number)
return a * b
return a * b
end</lang>
end</syntaxhighlight>


Julia also supports `assignment` definition as shorthand:
Julia also supports `assignment` definition as shorthand:


<lang julia>multiply(a, b) = a * b</lang>
<syntaxhighlight lang="julia">multiply(a, b) = a * b</syntaxhighlight>


And lambda calculus:
And lambda calculus:


<lang julia>multiply = (a, b) -> a * b</lang>
<syntaxhighlight lang="julia">multiply = (a, b) -> a * b</syntaxhighlight>


=={{header|Kaya}}==
=={{header|Kaya}}==
<lang kaya>program test;
<syntaxhighlight lang="kaya">program test;


// A function definition in Kaya:
// A function definition in Kaya:
Line 1,818: Line 1,818:
Void main() {
Void main() {
putStrLn(string( multiply(2, 3) ));
putStrLn(string( multiply(2, 3) ));
}</lang>
}</syntaxhighlight>


=={{header|Klingphix}}==
=={{header|Klingphix}}==
<lang Klingphix>:multiply * ;
<syntaxhighlight lang="klingphix">:multiply * ;


2 3 multiply print { 6 }</lang>
2 3 multiply print { 6 }</syntaxhighlight>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang kotlin>// One-liner
<syntaxhighlight lang="kotlin">// One-liner
fun multiply(a: Int, b: Int) = a * b
fun multiply(a: Int, b: Int) = a * b


Line 1,832: Line 1,832:
fun multiplyProper(a: Int, b: Int): Int {
fun multiplyProper(a: Int, b: Int): Int {
return a * b
return a * b
}</lang>
}</syntaxhighlight>


=={{header|Lambdatalk}}==
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
<lang Scheme>
{def multiply
{def multiply
{lambda {:a :b}
{lambda {:a :b}
Line 1,852: Line 1,852:
-> 720
-> 720


</syntaxhighlight>
</lang>


=={{header|langur}}==
=={{header|langur}}==
Line 1,865: Line 1,865:
=== explicit parameters ===
=== 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.
Explicit parameters are defined with parentheses after the f token, with no spacing. To specify no parameters, use an empty set of parentheses.
<lang langur>val .multiply = f(.x, .y) .x x .y
<syntaxhighlight lang="langur">val .multiply = f(.x, .y) .x x .y
.multiply(3, 4)</lang>
.multiply(3, 4)</syntaxhighlight>


=== implied parameters ===
=== 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.
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.
<lang langur>val .multiply = f .x x .y
<syntaxhighlight lang="langur">val .multiply = f .x x .y
.multiply(3, 4)</lang>
.multiply(3, 4)</syntaxhighlight>


=== operator implied functions ===
=== operator implied functions ===
Line 1,877: Line 1,877:


{{works with|langur|0.6.6}}
{{works with|langur|0.6.6}}
<lang langur>val .multiply = f{x}
<syntaxhighlight lang="langur">val .multiply = f{x}
.multiply(3, 4)</lang>
.multiply(3, 4)</syntaxhighlight>


=== nil left partially implied functions ===
=== nil left partially implied functions ===
Line 1,884: Line 1,884:


{{works with|langur|0.8.11}}
{{works with|langur|0.8.11}}
<lang langur>val .times3 = f{x 3}
<syntaxhighlight lang="langur">val .times3 = f{x 3}
map .times3, [1, 2, 3]</lang>
map .times3, [1, 2, 3]</syntaxhighlight>


=={{header|Lasso}}==
=={{header|Lasso}}==
Line 1,891: Line 1,891:
Lasso supports multiple dispatch — signature definitions determine which method will be invoked.
Lasso supports multiple dispatch — signature definitions determine which method will be invoked.


<lang Lasso>define multiply(a,b) => {
<syntaxhighlight lang="lasso">define multiply(a,b) => {
return #a * #b
return #a * #b
}</lang>
}</syntaxhighlight>


As this function is so simple it can also be represented like so:
As this function is so simple it can also be represented like so:


<lang Lasso>define multiply(a,b) => #a * #b</lang>
<syntaxhighlight lang="lasso">define multiply(a,b) => #a * #b</syntaxhighlight>


Using multiple dispatch, different functions will be invoked depending on the functions input.
Using multiple dispatch, different functions will be invoked depending on the functions input.


<lang Lasso>// Signatures that convert second input to match first input
<syntaxhighlight lang="lasso">// Signatures that convert second input to match first input
define multiply(a::integer,b::any) => #a * integer(#b)
define multiply(a::integer,b::any) => #a * integer(#b)
define multiply(a::decimal,b::any) => #a * decimal(#b)
define multiply(a::decimal,b::any) => #a * decimal(#b)


// Catch all signature
// Catch all signature
define multiply(a::any,b::any) => decimal(#a) * decimal(#b)</lang>
define multiply(a::any,b::any) => decimal(#a) * decimal(#b)</syntaxhighlight>


=={{header|Latitude}}==
=={{header|Latitude}}==
Line 1,912: Line 1,912:
Latitude methods are defined using curly braces <code>{}</code> and assigned to variables like any other value. Arguments are implicitly named <code>$1</code>, <code>$2</code>, etc.
Latitude methods are defined using curly braces <code>{}</code> and assigned to variables like any other value. Arguments are implicitly named <code>$1</code>, <code>$2</code>, etc.


<lang latitude>multiply := { $1 * $2. }.</lang>
<syntaxhighlight lang="latitude">multiply := { $1 * $2. }.</syntaxhighlight>


Calling a method is done either with parentheses or with a colon.
Calling a method is done either with parentheses or with a colon.


<lang latitude>multiply (2, 3).
<syntaxhighlight lang="latitude">multiply (2, 3).
multiply: 2, 3.</lang>
multiply: 2, 3.</syntaxhighlight>


If a method is intended to be used as a first-class value or stored in a data structure, the automatic evaluation behavior of methods can be undesired. In this case, one can wrap a method in a <code>Proc</code> with the <code>proc</code> method. <code>Proc</code> objects can then be later called explicitly with <code>call</code>.
If a method is intended to be used as a first-class value or stored in a data structure, the automatic evaluation behavior of methods can be undesired. In this case, one can wrap a method in a <code>Proc</code> with the <code>proc</code> method. <code>Proc</code> objects can then be later called explicitly with <code>call</code>.


<lang latitude>multiply := proc { $1 * $2. }.
<syntaxhighlight lang="latitude">multiply := proc { $1 * $2. }.
multiply call (2, 3).
multiply call (2, 3).
multiply call: 2, 3.</lang>
multiply call: 2, 3.</syntaxhighlight>


=={{header|LFE}}==
=={{header|LFE}}==
<lang lisp>
<syntaxhighlight lang="lisp">
(defun mutiply (a b)
(defun mutiply (a b)
(* a b))
(* a b))
</syntaxhighlight>
</lang>


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
<lang lb>' define & call a function
<syntaxhighlight lang="lb">' define & call a function


print multiply( 3, 1.23456)
print multiply( 3, 1.23456)
Line 1,942: Line 1,942:
end function
end function


end</lang>
end</syntaxhighlight>


=={{header|Lily}}==
=={{header|Lily}}==
<lang Lily>define multiply(a: Integer, b: Integer): Integer
<syntaxhighlight lang="lily">define multiply(a: Integer, b: Integer): Integer
{
{
return a * b
return a * b
}</lang>
}</syntaxhighlight>


=={{header|Lingo}}==
=={{header|Lingo}}==
<lang lingo>on multiply (a, b)
<syntaxhighlight lang="lingo">on multiply (a, b)
return a * b
return a * b
end</lang>
end</syntaxhighlight>


=={{header|LiveCode}}==
=={{header|LiveCode}}==
LiveCode has a built-in method called multiply, so there is an extra y to avoid an error.
LiveCode has a built-in method called multiply, so there is an extra y to avoid an error.
<lang LiveCode>function multiplyy n1 n2
<syntaxhighlight lang="livecode">function multiplyy n1 n2
return n1 * n2
return n1 * n2
end multiplyy
end multiplyy


put multiplyy(2,5) -- = 10</lang>
put multiplyy(2,5) -- = 10</syntaxhighlight>


=={{header|Locomotive Basic}}==
=={{header|Locomotive Basic}}==
<lang locobasic>10 DEF FNmultiply(x,y)=x*y
<syntaxhighlight lang="locobasic">10 DEF FNmultiply(x,y)=x*y
20 PRINT FNmultiply(2,PI)</lang>
20 PRINT FNmultiply(2,PI)</syntaxhighlight>
Function names are always preceded by "FN" in Locomotive BASIC. Also, PI is predefined by the interpreter as 3.14159265.
Function names are always preceded by "FN" in Locomotive BASIC. Also, PI is predefined by the interpreter as 3.14159265.


=={{header|Logo}}==
=={{header|Logo}}==
<lang logo>to multiply :x :y
<syntaxhighlight lang="logo">to multiply :x :y
output :x * :y
output :x * :y
end</lang>
end</syntaxhighlight>


=={{header|LSE64}}==
=={{header|LSE64}}==
<lang lse64>multiply : *
<syntaxhighlight lang="lse64">multiply : *
multiply. : *. # floating point</lang>
multiply. : *. # floating point</syntaxhighlight>


=={{header|Lua}}==
=={{header|Lua}}==
<lang Lua>function multiply( a, b )
<syntaxhighlight lang="lua">function multiply( a, b )
return a * b
return a * b
end</lang>
end</syntaxhighlight>


=={{header|Lucid}}==
=={{header|Lucid}}==
<lang lucid>multiply(x,y) = x * y</lang>
<syntaxhighlight lang="lucid">multiply(x,y) = x * y</syntaxhighlight>


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
===A Module can return value===
===A Module can return value===
A module can return value to stack of values. Calling a module we place parent stack to module, so we can read any value.
A module can return value to stack of values. Calling a module we place parent stack to module, so we can read any value.
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
Module Checkit {
Module Multiply (a, b) {
Module Multiply (a, b) {
Line 2,020: Line 2,020:
Call Checkit, 20, 50
Call Checkit, 20, 50
Print Number=1000
Print Number=1000
</syntaxhighlight>
</lang>


===A Local Function Definition===
===A Local Function Definition===


There are two types of function, the normal and the lambda. If a Function return string then we have to use $ at the end of function name.
There are two types of function, the normal and the lambda. If a Function return string then we have to use $ at the end of function name.
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
Module Checkit {
\\ functions can shange by using a newer definition
\\ functions can shange by using a newer definition
Line 2,091: Line 2,091:
}
}
Checkit
Checkit
</syntaxhighlight>
</lang>


===A Lambda Function===
===A Lambda Function===
Lambda function is first citizen. We can push it to stack and make another reading from stack. Lambda can use closures as static variables, some of them are pointers so if we copy a lambda we just copy the pointer. Pointers are containers like pointer to array, inventory and stack. Here we define string lambda function (there is a numeric also)
Lambda function is first citizen. We can push it to stack and make another reading from stack. Lambda can use closures as static variables, some of them are pointers so if we copy a lambda we just copy the pointer. Pointers are containers like pointer to array, inventory and stack. Here we define string lambda function (there is a numeric also)


<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
Module CheckIt {
A$=Lambda$ N$="Hello There" (x) ->{
A$=Lambda$ N$="Hello There" (x) ->{
Line 2,118: Line 2,118:
B$=List$("Hello", "Rosetta", "Function")
B$=List$("Hello", "Rosetta", "Function")
Print B$(1)="Hello"
Print B$(1)="Hello"
</syntaxhighlight>
</lang>


=={{header|M4}}==
=={{header|M4}}==
<lang M4>define(`multiply',`eval($1*$2)')
<syntaxhighlight lang="m4">define(`multiply',`eval($1*$2)')


multiply(2,3)</lang>
multiply(2,3)</syntaxhighlight>


=={{header|MAD}}==
=={{header|MAD}}==
MAD supports two types of function declarations. One simply evaluates an expression:
MAD supports two types of function declarations. One simply evaluates an expression:
<lang mad> INTERNAL FUNCTION MULT.(A,B) = A * B</lang>
<syntaxhighlight lang="mad"> INTERNAL FUNCTION MULT.(A,B) = A * B</syntaxhighlight>


Another allows multiple lines to be executed:
Another allows multiple lines to be executed:
<lang mad> INTERNAL FUNCTION(A, B)
<syntaxhighlight lang="mad"> INTERNAL FUNCTION(A, B)
ENTRY TO MULT.
ENTRY TO MULT.
FUNCTION RETURN A * B
FUNCTION RETURN A * B
END OF FUNCTION</lang>
END OF FUNCTION</syntaxhighlight>


There are several quirks here. First, the length of any identifier must not be longer than six
There are several quirks here. First, the length of any identifier must not be longer than six
Line 2,149: Line 2,149:
=={{header|Make}}==
=={{header|Make}}==
In makefile, a function may be defined as a rule, with recursive make used to retrieve the returned value.
In makefile, a function may be defined as a rule, with recursive make used to retrieve the returned value.
<lang make>A=1
<syntaxhighlight lang="make">A=1
B=1
B=1


multiply:
multiply:
@expr $(A) \* $(B)</lang>
@expr $(A) \* $(B)</syntaxhighlight>
Invoking it
Invoking it
<lang make>make -f mul.mk multiply A=100 B=3
<syntaxhighlight lang="make">make -f mul.mk multiply A=100 B=3
> 300</lang>
> 300</syntaxhighlight>
Using gmake, the define syntax is used to define a new function
Using gmake, the define syntax is used to define a new function
{{works with|gmake}}
{{works with|gmake}}
<lang make>A=1
<syntaxhighlight lang="make">A=1
B=1
B=1


Line 2,169: Line 2,169:
@$(call multiply, $(A), $(B))
@$(call multiply, $(A), $(B))


|gmake -f mul.mk do A=5 B=3</lang>
|gmake -f mul.mk do A=5 B=3</syntaxhighlight>


=={{header|Maple}}==
=={{header|Maple}}==
<lang maple>multiply:= (a, b) -> a * b;</lang>
<syntaxhighlight lang="maple">multiply:= (a, b) -> a * b;</syntaxhighlight>


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Line 2,178: Line 2,178:


Defining a function as a transformation rule:
Defining a function as a transformation rule:
<lang Mathematica>multiply[a_,b_]:=a*b</lang>
<syntaxhighlight lang="mathematica">multiply[a_,b_]:=a*b</syntaxhighlight>
Defining a pure function:
Defining a pure function:
<lang Mathematica>multiply=#1*#2&</lang>
<syntaxhighlight lang="mathematica">multiply=#1*#2&</syntaxhighlight>


=={{header|Maxima}}==
=={{header|Maxima}}==
<lang Maxima>f(a, b):= a*b;</lang>
<syntaxhighlight lang="maxima">f(a, b):= a*b;</syntaxhighlight>


=={{header|MAXScript}}==
=={{header|MAXScript}}==
<lang maxscript>fn multiply a b =
<syntaxhighlight lang="maxscript">fn multiply a b =
(
(
a * b
a * b
)</lang>
)</syntaxhighlight>


=={{header|Mercury}}==
=={{header|Mercury}}==
<lang Mercury>% Module ceremony elided...
<syntaxhighlight lang="mercury">% Module ceremony elided...
:- func multiply(integer, integer) = integer.
:- func multiply(integer, integer) = integer.
multiply(A, B) = A * B.</lang>
multiply(A, B) = A * B.</syntaxhighlight>


=={{header|Metafont}}==
=={{header|Metafont}}==
Metafont has macros, rather than functions; through those the language can be expanded. According to the kind of macro we are going to define, Metafont has different ways of doing it. The one suitable for this task is called <code>primarydef</code>.
Metafont has macros, rather than functions; through those the language can be expanded. According to the kind of macro we are going to define, Metafont has different ways of doing it. The one suitable for this task is called <code>primarydef</code>.
<lang metafont>primarydef a mult b = a * b enddef;</lang>
<syntaxhighlight lang="metafont">primarydef a mult b = a * b enddef;</syntaxhighlight>
<lang metafont>t := 3 mult 5; show t; end</lang>
<syntaxhighlight lang="metafont">t := 3 mult 5; show t; end</syntaxhighlight>
The '''primarydef''' allows to build binary operators with the same priority as *. For a more generic macro, we can use instead
The '''primarydef''' allows to build binary operators with the same priority as *. For a more generic macro, we can use instead
<lang metafont>def mult(expr a, b) = (a * b) enddef;
<syntaxhighlight lang="metafont">def mult(expr a, b) = (a * b) enddef;
t := mult(2,3);
t := mult(2,3);
show t;
show t;
end</lang>
end</syntaxhighlight>


=={{header|min}}==
=={{header|min}}==
<code>'*</code> is syntax sugar for <code>(*)</code>, which is an anonymous function that takes two numbers from the data stack, multiplies them, and leaves the result on the data stack. To give it a name, we can use the <code>:</code> sigil which is syntax sugar for <code>define</code>.
<code>'*</code> is syntax sugar for <code>(*)</code>, which is an anonymous function that takes two numbers from the data stack, multiplies them, and leaves the result on the data stack. To give it a name, we can use the <code>:</code> sigil which is syntax sugar for <code>define</code>.
<lang min>'* :multiply</lang>
<syntaxhighlight lang="min">'* :multiply</syntaxhighlight>


=={{header|MiniScript}}==
=={{header|MiniScript}}==
<lang MiniScript>multiply = function(x,y)
<syntaxhighlight lang="miniscript">multiply = function(x,y)
return x*y
return x*y
end function
end function


print multiply(6, 7)</lang>
print multiply(6, 7)</syntaxhighlight>
{{out}}
{{out}}
<pre>42</pre>
<pre>42</pre>
Line 2,233: Line 2,233:


=={{header|Modula-2}}==
=={{header|Modula-2}}==
<lang modula2>PROCEDURE Multiply(a, b: INTEGER): INTEGER;
<syntaxhighlight lang="modula2">PROCEDURE Multiply(a, b: INTEGER): INTEGER;
BEGIN
BEGIN
RETURN a * b
RETURN a * b
END Multiply;</lang>
END Multiply;</syntaxhighlight>


=={{header|Modula-3}}==
=={{header|Modula-3}}==
<lang modula3>PROCEDURE Multiply(a, b: INTEGER): INTEGER =
<syntaxhighlight lang="modula3">PROCEDURE Multiply(a, b: INTEGER): INTEGER =
BEGIN
BEGIN
RETURN a * b;
RETURN a * b;
END Multiply;</lang>
END Multiply;</syntaxhighlight>


=={{header|MUMPS}}==
=={{header|MUMPS}}==
<lang MUMPS>MULTIPLY(A,B);Returns the product of A and B
<syntaxhighlight lang="mumps">MULTIPLY(A,B);Returns the product of A and B
QUIT A*B</lang>
QUIT A*B</syntaxhighlight>


=={{header|Nanoquery}}==
=={{header|Nanoquery}}==
<lang nanoquery>def multiply(a, b)
<syntaxhighlight lang="nanoquery">def multiply(a, b)
return a * b
return a * b
end</lang>
end</syntaxhighlight>


=={{header|Neko}}==
=={{header|Neko}}==
<lang Neko>var multiply = function(a, b) {
<syntaxhighlight lang="neko">var multiply = function(a, b) {
a * b
a * b
}
}


$print(multiply(2, 3))</lang>
$print(multiply(2, 3))</syntaxhighlight>


'''Output:'''
'''Output:'''
Line 2,264: Line 2,264:


=={{header|Nemerle}}==
=={{header|Nemerle}}==
<lang Nemerle>public Multiply (a : int, b : int) : int // this is either a class or module method
<syntaxhighlight lang="nemerle">public Multiply (a : int, b : int) : int // this is either a class or module method
{
{
def multiply(a, b) { return a * b } // this is a local function, can take advantage of type inference
def multiply(a, b) { return a * b } // this is a local function, can take advantage of type inference
return multiply(a, b)
return multiply(a, b)
}</lang>
}</syntaxhighlight>


=={{header|NESL}}==
=={{header|NESL}}==
<lang nesl>function multiply(x, y) = x * y;</lang>
<syntaxhighlight lang="nesl">function multiply(x, y) = x * y;</syntaxhighlight>
The NESL system responds by reporting the type it has inferred for the function:
The NESL system responds by reporting the type it has inferred for the function:
<pre>multiply = fn : (a, a) -> a :: (a in number)</pre>
<pre>multiply = fn : (a, a) -> a :: (a in number)</pre>


=={{header|NetRexx}}==
=={{header|NetRexx}}==
<lang NetRexx>/* NetRexx */
<syntaxhighlight lang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols binary
options replace format comments java crossref savelog symbols binary


Line 2,298: Line 2,298:


product = multiplicand * multiplier
product = multiplicand * multiplier
return product</lang>
return product</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,307: Line 2,307:


=={{header|NewLISP}}==
=={{header|NewLISP}}==
<lang NewLISP>> (define (my-multiply a b) (* a b))
<syntaxhighlight lang="newlisp">> (define (my-multiply a b) (* a b))
(lambda (a b) (* a b))
(lambda (a b) (* a b))
> (my-multiply 2 3)
> (my-multiply 2 3)
6</lang>
6</syntaxhighlight>


=={{header|Nial}}==
=={{header|Nial}}==
Using variables
Using variables
<lang nial>multiply is operation a b {a * b}</lang>
<syntaxhighlight lang="nial">multiply is operation a b {a * b}</syntaxhighlight>
Using it
Using it
<lang nial>|multiply 2 3
<syntaxhighlight lang="nial">|multiply 2 3
=6</lang>
=6</syntaxhighlight>
Point free form
Point free form
<lang nial>mul is *</lang>
<syntaxhighlight lang="nial">mul is *</syntaxhighlight>
Using it
Using it
<lang nial>|mul 3 4
<syntaxhighlight lang="nial">|mul 3 4
=12</lang>
=12</syntaxhighlight>
Nial also allows creation of operators
Nial also allows creation of operators
<lang nial>multiply is op a b {a * b}</lang>
<syntaxhighlight lang="nial">multiply is op a b {a * b}</syntaxhighlight>
Using it.
Using it.
<lang nial>|2 multiply 3
<syntaxhighlight lang="nial">|2 multiply 3
=6
=6
|multiply 2 3
|multiply 2 3
=6</lang>
=6</syntaxhighlight>
Since this is an array programming language, any parameters can be arrays too
Since this is an array programming language, any parameters can be arrays too
<lang nial>|mul 3 [1,2]
<syntaxhighlight lang="nial">|mul 3 [1,2]
=3 6
=3 6
|mul [1,2] [10,20]
|mul [1,2] [10,20]
=10 40</lang>
=10 40</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
Nim has a magic variable, `result`, which can be used as a substitute for `return`. The `result` variable will be returned implicitly.
Nim has a magic variable, `result`, which can be used as a substitute for `return`. The `result` variable will be returned implicitly.
<lang nim>proc multiply(a, b: int): int =
<syntaxhighlight lang="nim">proc multiply(a, b: int): int =
result = a * b</lang>
result = a * b</syntaxhighlight>
Here is the same function but with the use of the `return` keyword.
Here is the same function but with the use of the `return` keyword.
<lang nim>proc multiply(a, b: int): int =
<syntaxhighlight lang="nim">proc multiply(a, b: int): int =
return a * b</lang>
return a * b</syntaxhighlight>
The last statement in a function implicitly is the result value:
The last statement in a function implicitly is the result value:
<lang nim>proc multiply(a, b: int): int = a * b</lang>
<syntaxhighlight lang="nim">proc multiply(a, b: int): int = a * b</syntaxhighlight>


=={{header|OASYS}}==
=={{header|OASYS}}==
<lang oasys_oac>method int multiply int x int y {
<syntaxhighlight lang="oasys_oac">method int multiply int x int y {
return x * y
return x * y
}</lang>
}</syntaxhighlight>


=={{header|OASYS Assembler}}==
=={{header|OASYS Assembler}}==
OASYS Assembler requires a prefix and suffix on names to indicate their types (an omitted suffix means a void type).
OASYS Assembler requires a prefix and suffix on names to indicate their types (an omitted suffix means a void type).
<lang oasys_oaa>[&MULTIPLY#,A#,B#],A#<,B#<MUL RF</lang>
<syntaxhighlight lang="oasys_oaa">[&MULTIPLY#,A#,B#],A#<,B#<MUL RF</syntaxhighlight>


=={{header|Oberon-2}}==
=={{header|Oberon-2}}==
Oberon-2 uses procedures, and has a special procedure called a "Function Procedure" used to return a value.
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;
<syntaxhighlight lang="oberon2">PROCEDURE Multiply(a, b: INTEGER): INTEGER;
BEGIN
BEGIN
RETURN a * b;
RETURN a * b;
END Multiply;</lang>
END Multiply;</syntaxhighlight>


=={{header|Objeck}}==
=={{header|Objeck}}==
<lang objeck>function : Multiply(a : Float, b : Float) ~, Float {
<syntaxhighlight lang="objeck">function : Multiply(a : Float, b : Float) ~, Float {
return a * b;
return a * b;
}</lang>
}</syntaxhighlight>


=={{header|OCaml}}==
=={{header|OCaml}}==


<lang ocaml>let int_multiply x y = x * y
<syntaxhighlight lang="ocaml">let int_multiply x y = x * y
let float_multiply x y = x *. y</lang>
let float_multiply x y = x *. y</syntaxhighlight>


=={{header|Octave}}==
=={{header|Octave}}==
<lang octave>function r = mult(a, b)
<syntaxhighlight lang="octave">function r = mult(a, b)
r = a .* b;
r = a .* b;
endfunction</lang>
endfunction</syntaxhighlight>


=={{header|Oforth}}==
=={{header|Oforth}}==
Line 2,383: Line 2,383:
If necessary, we can create a function with name multiply, but, it will just call *
If necessary, we can create a function with name multiply, but, it will just call *


<lang Oforth>: multiply * ;</lang>
<syntaxhighlight lang="oforth">: multiply * ;</syntaxhighlight>


It is also possible to create a function with declared paramaters. In this case, if we define n parameters, n objects will be removed from the stack and stored into those parameters :
It is also possible to create a function with declared paramaters. In this case, if we define n parameters, n objects will be removed from the stack and stored into those parameters :


<lang Oforth>: multiply2(a, b) a b * ;</lang>
<syntaxhighlight lang="oforth">: multiply2(a, b) a b * ;</syntaxhighlight>


A function return value (or values) is always what remains on the stack when the function ends. There is no syntax to define explicitely what is the return value(s) of a function.
A function return value (or values) is always what remains on the stack when the function ends. There is no syntax to define explicitely what is the return value(s) of a function.
Line 2,393: Line 2,393:
=={{header|Ol}}==
=={{header|Ol}}==
Function creation implemented using keyword 'lambda'. This created anonymous function can be saved into local or global variable for further use.
Function creation implemented using keyword 'lambda'. This created anonymous function can be saved into local or global variable for further use.
<lang scheme>
<syntaxhighlight lang="scheme">
(lambda (x y)
(lambda (x y)
(* x y))
(* x y))
</syntaxhighlight>
</lang>


Ol has two fully equal definitions of global named function (second one is syntactic sugar for first one). In fact both of them is saving the created lambda in global variable.
Ol has two fully equal definitions of global named function (second one is syntactic sugar for first one). In fact both of them is saving the created lambda in global variable.
<lang scheme>
<syntaxhighlight lang="scheme">
(define multiply (lambda (x y) (* x y)))
(define multiply (lambda (x y) (* x y)))


(define (multiply x y) (* x y))
(define (multiply x y) (* x y))
</syntaxhighlight>
</lang>


And only one definition of local named functions (with immediate calculation). This type of definition helps to implement local recursions.
And only one definition of local named functions (with immediate calculation). This type of definition helps to implement local recursions.
<lang scheme>
<syntaxhighlight lang="scheme">
(let multiply ((x n) (y m))
(let multiply ((x n) (y m))
(* x y))
(* x y))
Line 2,419: Line 2,419:
(print (multiply 7 8))
(print (multiply 7 8))
; ==> 56
; ==> 56
</syntaxhighlight>
</lang>


=={{header|OOC}}==
=={{header|OOC}}==
<lang ooc>
<syntaxhighlight lang="ooc">
multiply: func (a: Double, b: Double) -> Double {
multiply: func (a: Double, b: Double) -> Double {
a * b
a * b
}
}
</syntaxhighlight>
</lang>


=={{header|ooRexx}}==
=={{header|ooRexx}}==
===Internal Procedure===
===Internal Procedure===
<lang rexx>SAY multiply(5, 6)
<syntaxhighlight lang="rexx">SAY multiply(5, 6)
EXIT
EXIT
multiply:
multiply:
PROCEDURE
PROCEDURE
PARSE ARG x, y
PARSE ARG x, y
RETURN x*y</lang>
RETURN x*y</syntaxhighlight>
===::Routine Directive===
===::Routine Directive===
<lang oorexx>
<syntaxhighlight lang="oorexx">
say multiply(5, 6)
say multiply(5, 6)
::routine multiply
::routine multiply
use arg x, y
use arg x, y
return x *y </lang>
return x *y </syntaxhighlight>
===Accomodate large factors===
===Accomodate large factors===
<lang oorexx>say multiply(123456789,987654321)
<syntaxhighlight lang="oorexx">say multiply(123456789,987654321)
say multiply_long(123456789,987654321)
say multiply_long(123456789,987654321)
::routine multiply
::routine multiply
Line 2,451: Line 2,451:
use arg x, y
use arg x, y
Numeric Digits (length(x)+length(y))
Numeric Digits (length(x)+length(y))
return x *y </lang>
return x *y </syntaxhighlight>
{{out}}
{{out}}
<pre>1.21932631E+17
<pre>1.21932631E+17
Line 2,457: Line 2,457:


=={{header|OpenEdge/Progress}}==
=={{header|OpenEdge/Progress}}==
<lang Progress (Openedge ABL)>function multiply returns dec (a as dec , b as dec ):
<syntaxhighlight lang="progress (openedge abl)">function multiply returns dec (a as dec , b as dec ):
return a * b .
return a * b .
end.</lang>
end.</syntaxhighlight>


=={{header|Oz}}==
=={{header|Oz}}==
<lang oz>fun {Multiply X Y}
<syntaxhighlight lang="oz">fun {Multiply X Y}
X * Y
X * Y
end</lang>
end</syntaxhighlight>
Or by exploiting first-class functions:
Or by exploiting first-class functions:
<lang oz>Multiply = Number.'*'</lang>
<syntaxhighlight lang="oz">Multiply = Number.'*'</syntaxhighlight>


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<lang parigp>multiply(a,b)=a*b;</lang>
<syntaxhighlight lang="parigp">multiply(a,b)=a*b;</syntaxhighlight>
or
or
<lang parigp>multiply=(a,b)->a*b;</lang>
<syntaxhighlight lang="parigp">multiply=(a,b)->a*b;</syntaxhighlight>
Note that in both cases the <code>;</code> is part of the definition of the function, not of the function itself: it suppresses the output of the function body, but does not suppress the output of the function when called. To do that, either double the semicolon (which will suppress the output of both) or wrap in braces:
Note that in both cases the <code>;</code> is part of the definition of the function, not of the function itself: it suppresses the output of the function body, but does not suppress the output of the function when called. To do that, either double the semicolon (which will suppress the output of both) or wrap in braces:
<lang parigp>multiply={(a,b)->a*b;}</lang>
<syntaxhighlight lang="parigp">multiply={(a,b)->a*b;}</syntaxhighlight>
which will return a function which calculates but does not return the product.
which will return a function which calculates but does not return the product.


Line 2,479: Line 2,479:
''see also: [[#Delphi|Delphi]] and [[#Free Pascal|Free Pascal]]''
''see also: [[#Delphi|Delphi]] and [[#Free Pascal|Free Pascal]]''


<lang pascal>function multiply(a, b: real): real;
<syntaxhighlight lang="pascal">function multiply(a, b: real): real;
begin
begin
multiply := a * b
multiply := a * b
end;</lang>
end;</syntaxhighlight>
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.
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.
Many processors do not comply with this specification, though, and allow ''overwriting'' the return value ''multiple'' times.
Line 2,488: Line 2,488:
=={{header|Perl}}==
=={{header|Perl}}==
The most basic form:
The most basic form:
<lang perl>sub multiply { return $_[0] * $_[1] }</lang>
<syntaxhighlight lang="perl">sub multiply { return $_[0] * $_[1] }</syntaxhighlight>
or simply:
or simply:
<lang perl>sub multiply { $_[0] * $_[1] }</lang>
<syntaxhighlight lang="perl">sub multiply { $_[0] * $_[1] }</syntaxhighlight>
Arguments in Perl subroutines are passed in the <code>@_</code> array, and they can be accessed directly, first one as <code>$_[0]</code>, second one as <code>$_[1]</code>, etc. When the above function is called with only one or no arguments then the missing ones have an undefined value which is converted to 0 in multiplication.
Arguments in Perl subroutines are passed in the <code>@_</code> array, and they can be accessed directly, first one as <code>$_[0]</code>, second one as <code>$_[1]</code>, etc. When the above function is called with only one or no arguments then the missing ones have an undefined value which is converted to 0 in multiplication.


This is an example using [http://perldoc.perl.org/perlsub.html#Prototypes subroutine prototypes]:
This is an example using [http://perldoc.perl.org/perlsub.html#Prototypes subroutine prototypes]:
<lang perl>sub multiply( $$ )
<syntaxhighlight lang="perl">sub multiply( $$ )
{
{
my ($a, $b) = @_;
my ($a, $b) = @_;
return $a * $b;
return $a * $b;
}</lang>
}</syntaxhighlight>
The above subroutine can only be called with exactly two [http://perldoc.perl.org/perldata.html#Scalar-values scalar values] (two dollar signs in the signature) but those values may be not numbers or not even defined. The <code>@_</code> array is unpacked into <code>$a</code> and <code>$b</code> lexical variables, which are used later.
The above subroutine can only be called with exactly two [http://perldoc.perl.org/perldata.html#Scalar-values scalar values] (two dollar signs in the signature) but those values may be not numbers or not even defined. The <code>@_</code> array is unpacked into <code>$a</code> and <code>$b</code> lexical variables, which are used later.


The arguments can be automatically unpacked into lexical variables using the experimental signatures feature (in core as of 5.20):
The arguments can be automatically unpacked into lexical variables using the experimental signatures feature (in core as of 5.20):
<lang perl>use experimental 'signatures';
<syntaxhighlight lang="perl">use experimental 'signatures';
sub multiply ($x, $y) {
sub multiply ($x, $y) {
return $x * $y;
return $x * $y;
}</lang>
}</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
{{libheader|Phix/basics}}
{{libheader|Phix/basics}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">multiply</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">multiply</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">*</span><span style="color: #000000;">b</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">*</span><span style="color: #000000;">b</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


=={{header|Phixmonti}}==
=={{header|Phixmonti}}==
<lang Phixmonti>def multiply * enddef</lang>
<syntaxhighlight lang="phixmonti">def multiply * enddef</syntaxhighlight>


=={{header|PHL}}==
=={{header|PHL}}==


<lang phl>@Integer multiply(@Integer a, @Integer b) [
<syntaxhighlight lang="phl">@Integer multiply(@Integer a, @Integer b) [
return a * b;
return a * b;
]</lang>
]</syntaxhighlight>


=={{header|PHP}}==
=={{header|PHP}}==
<lang php>function multiply( $a, $b )
<syntaxhighlight lang="php">function multiply( $a, $b )
{
{
return $a * $b;
return $a * $b;
}</lang>
}</syntaxhighlight>


=={{header|Picat}}==
=={{header|Picat}}==
<lang php>multiply(A, B) = A*B.
<syntaxhighlight lang="php">multiply(A, B) = A*B.
</syntaxhighlight>
</lang>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(de multiply (A B)
<syntaxhighlight lang="picolisp">(de multiply (A B)
(* A B) )</lang>
(* A B) )</syntaxhighlight>


=={{header|Pike}}==
=={{header|Pike}}==
<lang pike>int multiply(int a, int b){
<syntaxhighlight lang="pike">int multiply(int a, int b){
return a * b;
return a * b;
}</lang>
}</syntaxhighlight>


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang pli>PRODUCT: procedure (a, b) returns (float);
<syntaxhighlight lang="pli">PRODUCT: procedure (a, b) returns (float);
declare (a, b) float;
declare (a, b) float;
return (a*b);
return (a*b);
end PRODUCT;</lang>
end PRODUCT;</syntaxhighlight>


=={{header|PL/SQL}}==
=={{header|PL/SQL}}==
<lang plsql>FUNCTION multiply(p_arg1 NUMBER, p_arg2 NUMBER) RETURN NUMBER
<syntaxhighlight lang="plsql">FUNCTION multiply(p_arg1 NUMBER, p_arg2 NUMBER) RETURN NUMBER
IS
IS
v_product NUMBER;
v_product NUMBER;
Line 2,557: Line 2,557:
v_product := p_arg1 * p_arg2;
v_product := p_arg1 * p_arg2;
RETURN v_product;
RETURN v_product;
END;</lang>
END;</syntaxhighlight>


=={{header|Plain English}}==
=={{header|Plain English}}==
The <code>Multiply a number by another number</code> routine is already defined in the noodle, so we need to tweak the wording slightly so the compiler doesn't complain about redefinition (or so the definition isn't recursive). Note that <code>the number</code> refers to the parameter <code>a number</code> and <code>the other number</code> refers to the parameter <code>another number</code>.
The <code>Multiply a number by another number</code> routine is already defined in the noodle, so we need to tweak the wording slightly so the compiler doesn't complain about redefinition (or so the definition isn't recursive). Note that <code>the number</code> refers to the parameter <code>a number</code> and <code>the other number</code> refers to the parameter <code>another number</code>.
<lang plainenglish>To multiply a number with another number:
<syntaxhighlight lang="plainenglish">To multiply a number with another number:
Multiply the number by the other number.</lang>
Multiply the number by the other number.</syntaxhighlight>


=={{header|Pop11}}==
=={{header|Pop11}}==
<lang pop11>define multiply(a, b);
<syntaxhighlight lang="pop11">define multiply(a, b);
a * b
a * b
enddefine;</lang>
enddefine;</syntaxhighlight>


=={{header|PostScript}}==
=={{header|PostScript}}==
Inbuilt:
Inbuilt:
<lang postscript>3 4 mul</lang>
<syntaxhighlight lang="postscript">3 4 mul</syntaxhighlight>
Function would be:
Function would be:
<lang postscript>/multiply{
<syntaxhighlight lang="postscript">/multiply{
/x exch def
/x exch def
/y exch def
/y exch def
x y mul =
x y mul =
}def</lang>
}def</syntaxhighlight>


=={{header|PowerShell}}==
=={{header|PowerShell}}==
The most basic variant of function definition would be the kind which uses positional parameters and therefore doesn't need to declare much:
The most basic variant of function definition would be the kind which uses positional parameters and therefore doesn't need to declare much:
<lang powershell>function multiply {
<syntaxhighlight lang="powershell">function multiply {
return $args[0] * $args[1]
return $args[0] * $args[1]
}</lang>
}</syntaxhighlight>
Also, the return statement can be omitted in many cases in PowerShell, since every value that "drops" out of a function can be used as a "return value":
Also, the return statement can be omitted in many cases in PowerShell, since every value that "drops" out of a function can be used as a "return value":
<lang powershell>function multiply {
<syntaxhighlight lang="powershell">function multiply {
$args[0] * $args[1]
$args[0] * $args[1]
}</lang>
}</syntaxhighlight>
Furthermore, the function arguments can be stated and named explicitly:
Furthermore, the function arguments can be stated and named explicitly:
<lang powershell>function multiply ($a, $b) {
<syntaxhighlight lang="powershell">function multiply ($a, $b) {
return $a * $b
return $a * $b
}</lang>
}</syntaxhighlight>
There is also an alternative style for declaring parameters. The choice is mostly a matter of personal preference:
There is also an alternative style for declaring parameters. The choice is mostly a matter of personal preference:
<lang powershell>function multiply {
<syntaxhighlight lang="powershell">function multiply {
param ($a, $b)
param ($a, $b)
return $a * $b
return $a * $b
}</lang>
}</syntaxhighlight>
And the arguments can have an explicit type:
And the arguments can have an explicit type:
<lang powershell>function multiply ([int] $a, [int] $b) {
<syntaxhighlight lang="powershell">function multiply ([int] $a, [int] $b) {
return $a * $b
return $a * $b
}</lang>
}</syntaxhighlight>


=={{header|Processing}}==
=={{header|Processing}}==
Processing is based on Java, and thus uses a familiar C-style syntax for function definition—as it does for much else. For the sake of argument, this implementation of <tt>multiply</tt> uses single-precision floats: other numeral types are available.
Processing is based on Java, and thus uses a familiar C-style syntax for function definition—as it does for much else. For the sake of argument, this implementation of <tt>multiply</tt> uses single-precision floats: other numeral types are available.
<lang java>float multiply(float x, float y)
<syntaxhighlight lang="java">float multiply(float x, float y)
{
{
return x * y;
return x * y;
}</lang>
}</syntaxhighlight>


==={{header|Processing Python mode}}===
==={{header|Processing Python mode}}===


Processing Python mode is based on Jython, a fully implemented Python 2 interpreter, and thus uses familiar Python syntax for function definition-as it does for much else.
Processing Python mode is based on Jython, a fully implemented Python 2 interpreter, and thus uses familiar Python syntax for function definition-as it does for much else.
<lang python>def multiply(x, y):
<syntaxhighlight lang="python">def multiply(x, y):
return x * y</lang>
return x * y</syntaxhighlight>


=={{header|Prolog}}==
=={{header|Prolog}}==
Prolog, as a logic programming languages, does not have user-supplied functions available. It has only predicates; statements which are "true" or "false". In cases where values have to be "returned" a parameter is passed in that is unified with the result. In the following predicate the parameter "P" (for "Product") is used in this role. The following code will work in any normal Prolog environment (but not in things like Turbo Prolog or Visual Prolog or their ilk):
Prolog, as a logic programming languages, does not have user-supplied functions available. It has only predicates; statements which are "true" or "false". In cases where values have to be "returned" a parameter is passed in that is unified with the result. In the following predicate the parameter "P" (for "Product") is used in this role. The following code will work in any normal Prolog environment (but not in things like Turbo Prolog or Visual Prolog or their ilk):
<lang Prolog>multiply(A, B, P) :- P is A * B.</lang>
<syntaxhighlight lang="prolog">multiply(A, B, P) :- P is A * B.</syntaxhighlight>
This is what it looks like in use:
This is what it looks like in use:
<lang Prolog>go :-
<syntaxhighlight lang="prolog">go :-
multiply(5, 2, P),
multiply(5, 2, P),
format("The product is ~d.~n", [P]).</lang>
format("The product is ~d.~n", [P]).</syntaxhighlight>
This can be a little bit jarring for those used to languages with implicit return values, but it has its advantages. For example unit testing of such a predicate doesn't require special frameworks to wrap the code:
This can be a little bit jarring for those used to languages with implicit return values, but it has its advantages. For example unit testing of such a predicate doesn't require special frameworks to wrap the code:
<lang Prolog>test_multiply :-
<syntaxhighlight lang="prolog">test_multiply :-
multiply(5, 2, 10), % this will pass
multiply(5, 2, 10), % this will pass
multiply(3, 4, 11). % this will not pass</lang>
multiply(3, 4, 11). % this will not pass</syntaxhighlight>
Still, the lack of user-defined functions remains an annoyance.
Still, the lack of user-defined functions remains an annoyance.


Prolog, however, is a remarkably malleable language and through its term re-writing capabilities the function-style approach could be emulated. The following code relies on the [http://packs.ndrix.com/function_expansion/index.html function_expansion] pack (separately installed through the packs system) for SWI-Prolog. Similar code could be made in any Prolog implementation, however.
Prolog, however, is a remarkably malleable language and through its term re-writing capabilities the function-style approach could be emulated. The following code relies on the [http://packs.ndrix.com/function_expansion/index.html function_expansion] pack (separately installed through the packs system) for SWI-Prolog. Similar code could be made in any Prolog implementation, however.
<lang Prolog>:- use_module(library(function_expansion)).
<syntaxhighlight lang="prolog">:- use_module(library(function_expansion)).


user:function_expansion(multiply(A, B), P, P is A * B). % "function" definition
user:function_expansion(multiply(A, B), P, P is A * B). % "function" definition


go :-
go :-
format("The product is ~d.~n", [multiply(5, 2)]).</lang>
format("The product is ~d.~n", [multiply(5, 2)]).</syntaxhighlight>


While the function '''definition''' is perhaps a bit more involved, the function '''use''' is now pretty much the same as any other language people are used to. The "magic" is accomplished by the compiler rewriting the <code>go/0</code> term into the following code:
While the function '''definition''' is perhaps a bit more involved, the function '''use''' is now pretty much the same as any other language people are used to. The "magic" is accomplished by the compiler rewriting the <code>go/0</code> term into the following code:
<lang Prolog>go :-
<syntaxhighlight lang="prolog">go :-
A is 5*2,
A is 5*2,
format('The product is ~d.~n', [A]).</lang>
format('The product is ~d.~n', [A]).</syntaxhighlight>


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>Procedure multiply(a,b)
<syntaxhighlight lang="purebasic">Procedure multiply(a,b)
ProcedureReturn a*b
ProcedureReturn a*b
EndProcedure</lang>
EndProcedure</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
Function definition:
Function definition:
<lang python>def multiply(a, b):
<syntaxhighlight lang="python">def multiply(a, b):
return a * b</lang>
return a * b</syntaxhighlight>
Lambda function definition:
Lambda function definition:
<lang python>multiply = lambda a, b: a * b</lang>
<syntaxhighlight lang="python">multiply = lambda a, b: a * b</syntaxhighlight>
A callable class definition allows functions and classes to use the same interface:
A callable class definition allows functions and classes to use the same interface:
<lang python>class Multiply:
<syntaxhighlight lang="python">class Multiply:
def __init__(self):
def __init__(self):
pass
pass
Line 2,660: Line 2,660:


multiply = Multiply()
multiply = Multiply()
print multiply(2, 4) # prints 8</lang>
print multiply(2, 4) # prints 8</syntaxhighlight>
(No extra functionality is shown in ''this'' class definition).
(No extra functionality is shown in ''this'' class definition).


=={{header|Q}}==
=={{header|Q}}==
<lang q>multiply:{[a;b] a*b}</lang>
<syntaxhighlight lang="q">multiply:{[a;b] a*b}</syntaxhighlight>
or
or
<lang q>multiply:{x*y}</lang>
<syntaxhighlight lang="q">multiply:{x*y}</syntaxhighlight>
or
or
<lang q>multiply:*</lang>
<syntaxhighlight lang="q">multiply:*</syntaxhighlight>
Using it
Using it
<lang q>multiply[2;3]
<syntaxhighlight lang="q">multiply[2;3]
6</lang>
6</syntaxhighlight>


=={{header|Quack}}==
=={{header|Quack}}==
You have several ways to define a function in Quack. You can do it by the classic way:
You have several ways to define a function in Quack. You can do it by the classic way:
<lang quack>fn multiply[ a; b ]
<syntaxhighlight lang="quack">fn multiply[ a; b ]
^ a * b
^ a * b
end</lang>
end</syntaxhighlight>


Using lambda-expressions:
Using lambda-expressions:
<lang quack>let multiply :- fn { a; b | a * b }</lang>
<syntaxhighlight lang="quack">let multiply :- fn { a; b | a * b }</syntaxhighlight>


And using partial anonymous functions:<lang quack>let multiply :- &(*)</lang>
And using partial anonymous functions:<syntaxhighlight lang="quack">let multiply :- &(*)</syntaxhighlight>


=={{header|Quackery}}==
=={{header|Quackery}}==
<lang quackery>[ * ] is multiply ( n n --> n )</lang>
<syntaxhighlight lang="quackery">[ * ] is multiply ( n n --> n )</syntaxhighlight>
In the Quackery shell (REPL):
In the Quackery shell (REPL):
<pre>
<pre>
Line 2,699: Line 2,699:


Words don't have to be named. We could have written the above as:
Words don't have to be named. We could have written the above as:
<lang quackery>2 ' [ * ] 3 swap do</lang>
<syntaxhighlight lang="quackery">2 ' [ * ] 3 swap do</syntaxhighlight>
By quoting the nest containing <code>*</code> with the <code>'</code> word, we have prevented it from being executed immediately and placed it on the data stack. Now it can be manipulated like any other nest or data stack object. We can use <code>do</code> to execute the contents of the nest.
By quoting the nest containing <code>*</code> with the <code>'</code> word, we have prevented it from being executed immediately and placed it on the data stack. Now it can be manipulated like any other nest or data stack object. We can use <code>do</code> to execute the contents of the nest.


=={{header|R}}==
=={{header|R}}==
<lang rsplus>mult <- function(a,b) a*b</lang>
<syntaxhighlight lang="rsplus">mult <- function(a,b) a*b</syntaxhighlight>
In general:
In general:
<lang rsplus>mult <- function(a,b) {
<syntaxhighlight lang="rsplus">mult <- function(a,b) {
a*b
a*b
# or:
# or:
# return(a*b)
# return(a*b)
}</lang>
}</syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==
A simple function definition that takes 2 arguments.
A simple function definition that takes 2 arguments.


<lang racket>(define (multiply a b) (* a b))</lang>
<syntaxhighlight lang="racket">(define (multiply a b) (* a b))</syntaxhighlight>


Using an explicit <code>lambda</code> or <code>λ</code> is completely equivalent:
Using an explicit <code>lambda</code> or <code>λ</code> is completely equivalent:
<lang racket>(define multiply (lambda (a b) (* a b)))</lang>
<syntaxhighlight lang="racket">(define multiply (lambda (a b) (* a b)))</syntaxhighlight>


<lang racket>(define multiply (λ (a b) (* a b)))</lang>
<syntaxhighlight lang="racket">(define multiply (λ (a b) (* a b)))</syntaxhighlight>


Note that <code>*</code> is a function value, so the following code also works (although <code>multiply</code> will now be variadic function).
Note that <code>*</code> is a function value, so the following code also works (although <code>multiply</code> will now be variadic function).


<lang racket>(define multiply *)</lang>
<syntaxhighlight lang="racket">(define multiply *)</syntaxhighlight>


=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
Without a signature:
Without a signature:
<lang perl6>sub multiply { return @_[0] * @_[1]; }</lang>
<syntaxhighlight lang="raku" line>sub multiply { return @_[0] * @_[1]; }</syntaxhighlight>
The return is optional on the final statement, since the last expression would return its value anyway. The final semicolon in a block is also optional.
The return is optional on the final statement, since the last expression would return its value anyway. The final semicolon in a block is also optional.
(Beware that a subroutine without an explicit signature, like this one, magically becomes variadic (rather than nullary) only if <code>@_</code> or <code>%_</code> appear in the body.) In fact, we can define the variadic version explicitly, which still works for two arguments:
(Beware that a subroutine without an explicit signature, like this one, magically becomes variadic (rather than nullary) only if <code>@_</code> or <code>%_</code> appear in the body.) In fact, we can define the variadic version explicitly, which still works for two arguments:
<lang perl6>sub multiply { [*] @_ }</lang>
<syntaxhighlight lang="raku" line>sub multiply { [*] @_ }</syntaxhighlight>
With formal parameters and a return type:
With formal parameters and a return type:
<lang perl6>sub multiply (Rat $a, Rat $b --> Rat) { $a * $b }</lang>
<syntaxhighlight lang="raku" line>sub multiply (Rat $a, Rat $b --> Rat) { $a * $b }</syntaxhighlight>
Same thing:
Same thing:
<lang perl6>my Rat sub multiply (Rat $a, Rat $b) { $a * $b }</lang>
<syntaxhighlight lang="raku" line>my Rat sub multiply (Rat $a, Rat $b) { $a * $b }</syntaxhighlight>
It is possible to define a function in "lambda" notation and then bind that into a scope, in which case it works like any function:
It is possible to define a function in "lambda" notation and then bind that into a scope, in which case it works like any function:
<lang perl6>my &multiply := -> $a, $b { $a * $b };</lang>
<syntaxhighlight lang="raku" line>my &multiply := -> $a, $b { $a * $b };</syntaxhighlight>
Another way to write a lambda is with internal placeholder parameters:
Another way to write a lambda is with internal placeholder parameters:
<lang perl6>my &multiply := { $^a * $^b };</lang>
<syntaxhighlight lang="raku" line>my &multiply := { $^a * $^b };</syntaxhighlight>
(And, in fact, our original <tt>@_</tt> above is just a variadic self-declaring placeholder argument. And the famous Perl "topic", <tt>$_</tt>, is just a self-declared parameter to a unary block.)
(And, in fact, our original <tt>@_</tt> above is just a variadic self-declaring placeholder argument. And the famous Perl "topic", <tt>$_</tt>, is just a self-declared parameter to a unary block.)


You may also curry both built-in and user-defined operators by supplying a <tt>*</tt> (known as "whatever") in place of the argument that is <i>not</i> to be curried:
You may also curry both built-in and user-defined operators by supplying a <tt>*</tt> (known as "whatever") in place of the argument that is <i>not</i> to be curried:
<lang perl6>my &multiply := * * *;</lang>
<syntaxhighlight lang="raku" line>my &multiply := * * *;</syntaxhighlight>
This is not terribly readable in this case due to the visual confusion between the whatever star and the multiplication operator, but Perl knows when it's expecting terms instead of infixes, so only the middle star is multiplication.
This is not terribly readable in this case due to the visual confusion between the whatever star and the multiplication operator, but Perl knows when it's expecting terms instead of infixes, so only the middle star is multiplication.
It tends to work out much better with other operators. In particular, you may
It tends to work out much better with other operators. In particular, you may
curry a cascade of methods with only the original invocant missing:
curry a cascade of methods with only the original invocant missing:
<lang perl6>@list.grep( *.substr(0,1).lc.match(/<[0..9 a..f]>/) )</lang>
<syntaxhighlight lang="raku" line>@list.grep( *.substr(0,1).lc.match(/<[0..9 a..f]>/) )</syntaxhighlight>
This is equivalent to:
This is equivalent to:
<lang perl6>@list.grep( -> $obj { $obj.substr(0,1).lc.match(/<[0..9 a..f]>/) } )</lang>
<syntaxhighlight lang="raku" line>@list.grep( -> $obj { $obj.substr(0,1).lc.match(/<[0..9 a..f]>/) } )</syntaxhighlight>


=={{header|Raven}}==
=={{header|Raven}}==
<lang raven>define multiply use a, b
<syntaxhighlight lang="raven">define multiply use a, b
a b *</lang>
a b *</syntaxhighlight>
Or optional infix:
Or optional infix:
<lang raven>define multiply use a, b
<syntaxhighlight lang="raven">define multiply use a, b
(a * b)</lang>
(a * b)</syntaxhighlight>
Or skip named vars:
Or skip named vars:
<lang raven>define multiply *</lang>
<syntaxhighlight lang="raven">define multiply *</syntaxhighlight>


=={{header|REALbasic}}==
=={{header|REALbasic}}==
<syntaxhighlight lang="vb">
<lang vb>
Function Multiply(a As Integer, b As Integer) As Integer
Function Multiply(a As Integer, b As Integer) As Integer
Return a * b
Return a * b
End Function
End Function
</syntaxhighlight>
</lang>


=={{header|REBOL}}==
=={{header|REBOL}}==
REBOL actually already has a function called 'multiply', which is a native compiled function. However, since it's not protected, I can easily override it:
REBOL actually already has a function called 'multiply', which is a native compiled function. However, since it's not protected, I can easily override it:
<lang REBOL>multiply: func [a b][a * b]</lang>
<syntaxhighlight lang="rebol">multiply: func [a b][a * b]</syntaxhighlight>


=={{header|Relation}}==
=={{header|Relation}}==
<syntaxhighlight lang="relation">
<lang Relation>
function multiply(a,b)
function multiply(a,b)
set result = a*b
set result = a*b
end function
end function
</syntaxhighlight>
</lang>


=={{header|Retro}}==
=={{header|Retro}}==
<lang Retro>: multiply ( nn-n ) * ;</lang>
<syntaxhighlight lang="retro">: multiply ( nn-n ) * ;</syntaxhighlight>


=={{header|REXX}}==
=={{header|REXX}}==
===exactitudeness===
===exactitudeness===
<lang rexx>multiply: return arg(1) * arg(2) /*return the product of the two arguments.*/</lang>
<syntaxhighlight lang="rexx">multiply: return arg(1) * arg(2) /*return the product of the two arguments.*/</syntaxhighlight>


===cleaner display===
===cleaner display===
Line 2,789: Line 2,789:
<br><br>I.E.: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ''' 3.0 * 4.00 ''' &nbsp; &nbsp; yields the product: &nbsp; &nbsp; '''12.000'''
<br><br>I.E.: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ''' 3.0 * 4.00 ''' &nbsp; &nbsp; yields the product: &nbsp; &nbsp; '''12.000'''
<br><br>This version eliminates the &nbsp; '''.000''' &nbsp; from the product.
<br><br>This version eliminates the &nbsp; '''.000''' &nbsp; from the product.
<lang rexx>multiply: return arg(1) * arg(2) / 1 /*return with a normalized product of 2 args. */</lang>
<syntaxhighlight lang="rexx">multiply: return arg(1) * arg(2) / 1 /*return with a normalized product of 2 args. */</syntaxhighlight>


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
func multiply x,y return x*y
func multiply x,y return x*y
</syntaxhighlight>
</lang>


=={{header|RLaB}}==
=={{header|RLaB}}==
In RLaB the functions can be built-in (compiled within RLaB, or part of the shared object library that is loaded per request of user), or user (written in RLaB script). Consider an example:
In RLaB the functions can be built-in (compiled within RLaB, or part of the shared object library that is loaded per request of user), or user (written in RLaB script). Consider an example:
<lang RLaB>>> class(sin)
<syntaxhighlight lang="rlab">>> class(sin)
function
function
>> type(sin)
>> type(sin)
builtin</lang>
builtin</syntaxhighlight>
Functions are a data class on their own, or they can be member of a list (associative array).
Functions are a data class on their own, or they can be member of a list (associative array).


1. user function specified from built-in functions, here basic addition
1. user function specified from built-in functions, here basic addition
<lang RLaB>f = function(x, y)
<syntaxhighlight lang="rlab">f = function(x, y)
{
{
return x + y;
return x + y;
Line 2,813: Line 2,813:
function
function
>> type(f)
>> type(f)
user</lang>
user</syntaxhighlight>


2. function can be member of a list (associative array)
2. function can be member of a list (associative array)
<lang RLaB>somelist = <<>>;
<syntaxhighlight lang="rlab">somelist = <<>>;
somelist.f = function(x, y)
somelist.f = function(x, y)
{
{
rval = x + y;
rval = x + y;
return rval;
return rval;
};</lang>
};</syntaxhighlight>


3. user function which uses a function that is specified as a member of some list, here we use ''somelist'' from above:
3. user function which uses a function that is specified as a member of some list, here we use ''somelist'' from above:
<lang RLaB>g = function(x, y)
<syntaxhighlight lang="rlab">g = function(x, y)
{
{
global(somelist);
global(somelist);
rval = x * somelist.f(x, 2*y);
rval = x * somelist.f(x, 2*y);
return rval;
return rval;
};</lang>
};</syntaxhighlight>


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>def multiply(a, b)
<syntaxhighlight lang="ruby">def multiply(a, b)
a * b
a * b
end</lang>
end</syntaxhighlight>


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>fn multiply(a: i32, b: i32) -> i32 {
<syntaxhighlight lang="rust">fn multiply(a: i32, b: i32) -> i32 {
a * b
a * b
}</lang>
}</syntaxhighlight>


=={{header|S-BASIC}}==
=={{header|S-BASIC}}==
S-BASIC is unusual in that the function return value is assigned to the END statement that terminates the function.
S-BASIC is unusual in that the function return value is assigned to the END statement that terminates the function.
<lang basic>
<syntaxhighlight lang="basic">
function multiply(a, b = real) = real
function multiply(a, b = real) = real
end = a * b
end = a * b
</syntaxhighlight>
</lang>


=={{header|Sather}}==
=={{header|Sather}}==
<lang sather>class MAIN is
<syntaxhighlight lang="sather">class MAIN is
-- we cannot have "functions" (methods) outside classes
-- we cannot have "functions" (methods) outside classes
mult(a, b:FLT):FLT is return a*b; end;
mult(a, b:FLT):FLT is return a*b; end;
Line 2,856: Line 2,856:
#OUT + mult(5.2, 3.4) + "\n";
#OUT + mult(5.2, 3.4) + "\n";
end;
end;
end;</lang>
end;</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
<lang scala>def multiply(a: Int, b: Int) = a * b</lang>
<syntaxhighlight lang="scala">def multiply(a: Int, b: Int) = a * b</syntaxhighlight>


=={{header|Scheme}}==
=={{header|Scheme}}==
<lang scheme>(define multiply *)</lang>
<syntaxhighlight lang="scheme">(define multiply *)</syntaxhighlight>
Alternately,
Alternately,
<lang scheme>(define (multiply a b)
<syntaxhighlight lang="scheme">(define (multiply a b)
(* a b))</lang>
(* a b))</syntaxhighlight>


=={{header|Seed7}}==
=={{header|Seed7}}==
<lang seed7>const func float: multiply (in float: a, in float: b) is
<syntaxhighlight lang="seed7">const func float: multiply (in float: a, in float: b) is
return a * b;</lang>
return a * b;</syntaxhighlight>


=={{header|SenseTalk}}==
=={{header|SenseTalk}}==
<lang sensetalk>put multiply(3,7) as words
<syntaxhighlight lang="sensetalk">put multiply(3,7) as words


to multiply num1, num2
to multiply num1, num2
return num1 * num2
return num1 * num2
end multiply
end multiply
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,884: Line 2,884:


=={{header|SETL}}==
=={{header|SETL}}==
<lang setl>proc multiply( a, b );
<syntaxhighlight lang="setl">proc multiply( a, b );
return a * b;
return a * b;
end proc;</lang>
end proc;</syntaxhighlight>


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func multiply(a, b) {
<syntaxhighlight lang="ruby">func multiply(a, b) {
a * b;
a * b;
}</lang>
}</syntaxhighlight>


=={{header|Simula}}==
=={{header|Simula}}==
Simula uses the term <tt>procedure</tt> for subroutines/methods whether they return a value or not. A procedure that does return a value is declared with a data type (e.g. <tt>integer procedure</tt>), whereas one that does not is declared simply as <tt>procedure</tt>. This program defines <tt>multiply</tt> as an integer procedure and illustrates its use. Note that the second argument provided to <tt>Outint</tt> gives the width of the integer to be printed.
Simula uses the term <tt>procedure</tt> for subroutines/methods whether they return a value or not. A procedure that does return a value is declared with a data type (e.g. <tt>integer procedure</tt>), whereas one that does not is declared simply as <tt>procedure</tt>. This program defines <tt>multiply</tt> as an integer procedure and illustrates its use. Note that the second argument provided to <tt>Outint</tt> gives the width of the integer to be printed.
<lang simula>BEGIN
<syntaxhighlight lang="simula">BEGIN
INTEGER PROCEDURE multiply(x, y);
INTEGER PROCEDURE multiply(x, y);
INTEGER x, y;
INTEGER x, y;
Line 2,903: Line 2,903:
Outint(multiply(7,8), 2);
Outint(multiply(7,8), 2);
Outimage
Outimage
END</lang>
END</syntaxhighlight>


=={{header|Slate}}==
=={{header|Slate}}==
<lang slate>define: #multiply -> [| :a :b | a * b].</lang>
<syntaxhighlight lang="slate">define: #multiply -> [| :a :b | a * b].</syntaxhighlight>
or using a macro:
or using a macro:
<lang slate>define: #multiply -> #* `er.</lang>
<syntaxhighlight lang="slate">define: #multiply -> #* `er.</syntaxhighlight>
The block may also be installed as a method like so:
The block may also be installed as a method like so:
<lang slate>a@(Number traits) multiplyBy: b@(Number traits) [a * b].</lang>
<syntaxhighlight lang="slate">a@(Number traits) multiplyBy: b@(Number traits) [a * b].</syntaxhighlight>
or more explicitly (without sugar):
or more explicitly (without sugar):
<lang slate>[| :a :b | a * b] asMethod: #multipleBy: on: {Number traits. Number traits}.</lang>
<syntaxhighlight lang="slate">[| :a :b | a * b] asMethod: #multipleBy: on: {Number traits. Number traits}.</syntaxhighlight>


=={{header|Smalltalk}}==
=={{header|Smalltalk}}==
<lang smalltalk>|mul|
<syntaxhighlight lang="smalltalk">|mul|
mul := [ :a :b | a * b ].</lang>
mul := [ :a :b | a * b ].</syntaxhighlight>


=={{header|SNOBOL4}}==
=={{header|SNOBOL4}}==
<lang snobol4> define('multiply(a,b)') :(mul_end)
<syntaxhighlight lang="snobol4"> define('multiply(a,b)') :(mul_end)
multiply multiply = a * b :(return)
multiply multiply = a * b :(return)
mul_end
mul_end
Line 2,925: Line 2,925:
output = multiply(10.1,12.2)
output = multiply(10.1,12.2)
output = multiply(10,12)
output = multiply(10,12)
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
123.22
123.22
Line 2,932: Line 2,932:
=={{header|SNUSP}}==
=={{header|SNUSP}}==
For expediency, the function is adding three values, instead of multiplying two values. Another function, atoi (+48) is called before printing the result.
For expediency, the function is adding three values, instead of multiplying two values. Another function, atoi (+48) is called before printing the result.
<lang snusp>+1>++2=@\=>+++3=@\==@\=.=# prints '6'
<syntaxhighlight lang="snusp">+1>++2=@\=>+++3=@\==@\=.=# prints '6'
| | \=itoa=@@@+@+++++#
| | \=itoa=@@@+@+++++#
\=======!\==!/===?\<#
\=======!\==!/===?\<#
\>+<-/</lang>
\>+<-/</syntaxhighlight>


=={{header|SPARK}}==
=={{header|SPARK}}==
The function definition (multiplies two standard Integer):
The function definition (multiplies two standard Integer):
<lang Ada>package Functions is
<syntaxhighlight lang="ada">package Functions is
function Multiply (A, B : Integer) return Integer;
function Multiply (A, B : Integer) return Integer;
--# pre A * B in Integer; -- See note below
--# pre A * B in Integer; -- See note below
--# return A * B; -- Implies commutativity on Multiply arguments
--# return A * B; -- Implies commutativity on Multiply arguments
end Functions;</lang>
end Functions;</syntaxhighlight>
Note: how do you ensure then “A * B in Integer” ? Either with a proof prior to Multiply invokation or using another form of Multiply where input A and B would be restricted to a range which ensures the resulting product is always valid. Exemple :
Note: how do you ensure then “A * B in Integer” ? Either with a proof prior to Multiply invokation or using another form of Multiply where input A and B would be restricted to a range which ensures the resulting product is always valid. Exemple :
<lang Ada>type Input_Type is range 0 .. 10;
<syntaxhighlight lang="ada">type Input_Type is range 0 .. 10;
type Result_Type is range 0 .. 100;</lang>
type Result_Type is range 0 .. 100;</syntaxhighlight>
and had a version of Multiply using these types. On the other hand, if arguments of Multiply are constants, this is provable straight away.
and had a version of Multiply using these types. On the other hand, if arguments of Multiply are constants, this is provable straight away.


The Multiply's implementation:
The Multiply's implementation:
<lang Ada>package body Functions is
<syntaxhighlight lang="ada">package body Functions is
function Multiply (A, B : Integer) return Integer is
function Multiply (A, B : Integer) return Integer is
begin
begin
return A * B;
return A * B;
end Multiply;
end Multiply;
end Functions;</lang>
end Functions;</syntaxhighlight>


=={{header|SPL}}==
=={{header|SPL}}==
Single-line function definition:
Single-line function definition:
<lang spl>multiply(a,b) <= a*b</lang>
<syntaxhighlight lang="spl">multiply(a,b) <= a*b</syntaxhighlight>
Multi-line function definition:
Multi-line function definition:
<lang spl>multiply(a,b)=
<syntaxhighlight lang="spl">multiply(a,b)=
x = a*b
x = a*b
<= x
<= x
.</lang>
.</syntaxhighlight>


=={{header|SSEM}}==
=={{header|SSEM}}==
Line 2,970: Line 2,970:


In this example, the main routine does nothing at all beyond calling the subroutine and halting after it has returned. The values <tt>A</tt> and <tt>B</tt> are passed in the two addresses located immediately before the subroutine begins; their product is returned in the address that formerly stored <tt>A</tt>. Given that the <tt>multiply</tt> subroutine begins at address 8, the calling routine looks like this:
In this example, the main routine does nothing at all beyond calling the subroutine and halting after it has returned. The values <tt>A</tt> and <tt>B</tt> are passed in the two addresses located immediately before the subroutine begins; their product is returned in the address that formerly stored <tt>A</tt>. Given that the <tt>multiply</tt> subroutine begins at address 8, the calling routine looks like this:
<lang ssem>01000000000000100000000000000000 0. -2 to c
<syntaxhighlight lang="ssem">01000000000000100000000000000000 0. -2 to c
00100000000000000000000000000000 1. 4 to CI
00100000000000000000000000000000 1. 4 to CI
01111111111111111111111111111111 2. -2
01111111111111111111111111111111 2. -2
00000000000001110000000000000000 3. Stop
00000000000001110000000000000000 3. Stop
11100000000000000000000000000000 4. 7</lang>
11100000000000000000000000000000 4. 7</syntaxhighlight>
or in pseudocode:
or in pseudocode:
<pre> load &here
<pre> load &here
Line 2,980: Line 2,980:
here: halt</pre>
here: halt</pre>
Implementing <tt>multiply</tt> on the SSEM requires the use of repeated negation and subtraction. For the sake of example, the values 8 and 7 are provided for <tt>A</tt> and <tt>B</tt>.
Implementing <tt>multiply</tt> on the SSEM requires the use of repeated negation and subtraction. For the sake of example, the values 8 and 7 are provided for <tt>A</tt> and <tt>B</tt>.
<lang ssem>00010000000000000000000000000000 6. 8
<syntaxhighlight lang="ssem">00010000000000000000000000000000 6. 8
11100000000000000000000000000000 7. 7
11100000000000000000000000000000 7. 7
11111000000001100000000000000000 8. c to 31
11111000000001100000000000000000 8. c to 31
Line 3,005: Line 3,005:
00110000000000000000000000000000 29. 12
00110000000000000000000000000000 29. 12
00000000000000000000000000000000 30. 0
00000000000000000000000000000000 30. 0
00000000000000000000000000000000 31. 0</lang>
00000000000000000000000000000000 31. 0</syntaxhighlight>
The pseudocode equivalent clarifies how the subroutine works, or how it would work on an architecture that supported <tt>load</tt> and <tt>add</tt>:
The pseudocode equivalent clarifies how the subroutine works, or how it would work on an architecture that supported <tt>load</tt> and <tt>add</tt>:
<pre>a: equals #8
<pre>a: equals #8
Line 3,026: Line 3,026:


=={{header|Standard ML}}==
=={{header|Standard ML}}==
<lang ocaml>val multiply = op *</lang>
<syntaxhighlight lang="ocaml">val multiply = op *</syntaxhighlight>
Equivalently,
Equivalently,
<lang ocaml>fun multiply (x, y) = x * y</lang>
<syntaxhighlight lang="ocaml">fun multiply (x, y) = x * y</syntaxhighlight>
Using lambda syntax:
Using lambda syntax:
<lang sml>val multiply = fn (x, y) => x * y</lang>
<syntaxhighlight lang="sml">val multiply = fn (x, y) => x * y</syntaxhighlight>
Curried form:
Curried form:
<lang ocaml>fun multiply x y = x * y</lang>
<syntaxhighlight lang="ocaml">fun multiply x y = x * y</syntaxhighlight>


=={{header|Stata}}==
=={{header|Stata}}==
Line 3,039: Line 3,039:
Stata's macro language does not have functions, but commands. Output is usually saved as a "stored result" (but could also be saved in a global macro variable, in a scalar or matrix, in a dataset or simply printed to the Results window). See '''[https://www.stata.com/help.cgi?program program]''' and '''[https://www.stata.com/help.cgi?return]''' in Stata documentation.
Stata's macro language does not have functions, but commands. Output is usually saved as a "stored result" (but could also be saved in a global macro variable, in a scalar or matrix, in a dataset or simply printed to the Results window). See '''[https://www.stata.com/help.cgi?program program]''' and '''[https://www.stata.com/help.cgi?return]''' in Stata documentation.


<lang stata>prog def multiply, return
<syntaxhighlight lang="stata">prog def multiply, return
args a b
args a b
return sca product=`a'*`b'
return sca product=`a'*`b'
Line 3,045: Line 3,045:


multiply 77 13
multiply 77 13
di r(product)</lang>
di r(product)</syntaxhighlight>


'''Output'''
'''Output'''
Line 3,054: Line 3,054:
Mata is the matrix language of Stata. Here is how to define a function
Mata is the matrix language of Stata. Here is how to define a function


<lang stata>mata
<syntaxhighlight lang="stata">mata
scalar multiply(scalar x, scalar y) {
scalar multiply(scalar x, scalar y) {
return(x*y)
return(x*y)
Line 3,060: Line 3,060:


multiply(77,13)
multiply(77,13)
end</lang>
end</syntaxhighlight>


'''Output'''
'''Output'''
Line 3,067: Line 3,067:


=={{header|Swift}}==
=={{header|Swift}}==
<lang swift>func multiply(a: Double, b: Double) -> Double {
<syntaxhighlight lang="swift">func multiply(a: Double, b: Double) -> Double {
return a * b
return a * b
}</lang>
}</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
Strictly as described in the task:
Strictly as described in the task:
<lang tcl>proc multiply { arg1 arg2 } {
<syntaxhighlight lang="tcl">proc multiply { arg1 arg2 } {
return [expr {$arg1 * $arg2}]
return [expr {$arg1 * $arg2}]
}</lang>
}</syntaxhighlight>
{{works with|Tcl|8.5}}
{{works with|Tcl|8.5}}
You can also create functions that work directly inside expressions. This is done by creating the command with the correct name (that is, in the ''tcl::mathfunc'' namespace):
You can also create functions that work directly inside expressions. This is done by creating the command with the correct name (that is, in the ''tcl::mathfunc'' namespace):
<lang tcl>proc tcl::mathfunc::multiply {arg1 arg2} {
<syntaxhighlight lang="tcl">proc tcl::mathfunc::multiply {arg1 arg2} {
return [expr {$arg1 * $arg2}]
return [expr {$arg1 * $arg2}]
}
}
Line 3,085: Line 3,085:
if {multiply(6, 9) == 42} {
if {multiply(6, 9) == 42} {
puts "Welcome, Citizens of Golgafrincham from the B-Ark!"
puts "Welcome, Citizens of Golgafrincham from the B-Ark!"
}</lang>
}</syntaxhighlight>


=={{header|TI-89 BASIC}}==
=={{header|TI-89 BASIC}}==
<lang ti89b>multiply(a, b)
<syntaxhighlight lang="ti89b">multiply(a, b)
Func
Func
Return a * b
Return a * b
EndFunc</lang>
EndFunc</syntaxhighlight>


=={{header|Toka}}==
=={{header|Toka}}==
<lang toka>[ ( ab-c ) * ] is multiply</lang>
<syntaxhighlight lang="toka">[ ( ab-c ) * ] is multiply</syntaxhighlight>


=={{header|Transd}}==
=={{header|Transd}}==
<lang scheme>multiply: (lambda a Double() b Double() (* a b))</lang>
<syntaxhighlight lang="scheme">multiply: (lambda a Double() b Double() (* a b))</syntaxhighlight>


=={{header|TXR}}==
=={{header|TXR}}==
Line 3,103: Line 3,103:


Here is how to make a pattern function that multiplies, and call it. To multiply the numbers, we break out of the pattern language and invoke Lisp evaluation: <code>@(* a b)</code>
Here is how to make a pattern function that multiplies, and call it. To multiply the numbers, we break out of the pattern language and invoke Lisp evaluation: <code>@(* a b)</code>
<lang txr>@(define multiply (a b out))
<syntaxhighlight lang="txr">@(define multiply (a b out))
@(bind out @(* a b))
@(bind out @(* a b))
@(end)
@(end)
@(multiply 3 4 result)</lang>
@(multiply 3 4 result)</syntaxhighlight>
<pre>$ txr -B multiply.txr
<pre>$ txr -B multiply.txr
result="12"</pre>
result="12"</pre>
In the embedded Lisp dialect, it is possible to write an ordinary function that returns a value:
In the embedded Lisp dialect, it is possible to write an ordinary function that returns a value:
<lang txrlisp>(defun mult (a b) (* a b))
<syntaxhighlight lang="txrlisp">(defun mult (a b) (* a b))
(put-line `3 * 4 = @(mult 3 4)`)</lang>
(put-line `3 * 4 = @(mult 3 4)`)</syntaxhighlight>
<pre>$ txr multiply.tl
<pre>$ txr multiply.tl
3 * 4 = 12</pre>
3 * 4 = 12</pre>
Line 3,117: Line 3,117:
=={{header|uBasic/4tH}}==
=={{header|uBasic/4tH}}==
In uBasic you can turn any subroutine into a function with the '''FUNC()''' function. It takes one argument, which is the label. Arguments are optional.
In uBasic you can turn any subroutine into a function with the '''FUNC()''' function. It takes one argument, which is the label. Arguments are optional.
<lang>PRINT FUNC (_Multiply (2,3))
<syntaxhighlight lang="text">PRINT FUNC (_Multiply (2,3))
END
END


_Multiply PARAM (2)
_Multiply PARAM (2)
RETURN (a@ * b@)</lang>
RETURN (a@ * b@)</syntaxhighlight>


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
Note that in the Unix shell, function definitions do not include any argument specifications within the parentheses. Instead arguments to functions are obtained using the positional parameters.
Note that in the Unix shell, function definitions do not include any argument specifications within the parentheses. Instead arguments to functions are obtained using the positional parameters.
{{works with|Bourne Shell}}
{{works with|Bourne Shell}}
<lang bash>multiply() {
<syntaxhighlight lang="bash">multiply() {
# There is never anything between the parentheses after the function name
# There is never anything between the parentheses after the function name
# Arguments are obtained using the positional parameters $1, and $2
# Arguments are obtained using the positional parameters $1, and $2
Line 3,135: Line 3,135:
# Call the function
# Call the function
multiply 3 4 # The function is invoked in statement context
multiply 3 4 # The function is invoked in statement context
echo $? # The dollarhook special variable gives the return value</lang>
echo $? # The dollarhook special variable gives the return value</syntaxhighlight>
{{works with|Bash}}
{{works with|Bash}}
return an exit code
return an exit code
<lang bash>multiply() {
<syntaxhighlight lang="bash">multiply() {
return $(($1 * $2))
return $(($1 * $2))
}
}
multiply 5 6
multiply 5 6
echo $?</lang>
echo $?</syntaxhighlight>
echo the result
echo the result
<lang bash>multiply() {
<syntaxhighlight lang="bash">multiply() {
echo -n $(($1 * $2))
echo -n $(($1 * $2))
}
}
echo $(multiply 5 6)</lang>
echo $(multiply 5 6)</syntaxhighlight>


=={{header|Ursa}}==
=={{header|Ursa}}==
<lang ursa># multiply is a built-in in ursa, so the function is called mult instead
<syntaxhighlight lang="ursa"># multiply is a built-in in ursa, so the function is called mult instead
def mult (int a, int b)
def mult (int a, int b)
return (* a b)
return (* a b)
end</lang>
end</syntaxhighlight>


=={{header|Ursala}}==
=={{header|Ursala}}==
Line 3,161: Line 3,161:
They may be specified by lambda abstraction, with dummy variables in double quotes, or in point-free form, or any combination. The way multiplication is defined depends on the type of numbers being multiplied. For this example, numbers in standard IEEE double precision are assumed, and the multiply function is defined in terms of the system library function, called using the syntax <code>math..mul</code>.
They may be specified by lambda abstraction, with dummy variables in double quotes, or in point-free form, or any combination. The way multiplication is defined depends on the type of numbers being multiplied. For this example, numbers in standard IEEE double precision are assumed, and the multiply function is defined in terms of the system library function, called using the syntax <code>math..mul</code>.
This is the definition in point free form,
This is the definition in point free form,
<lang Ursala>multiply = math..mul</lang>
<syntaxhighlight lang="ursala">multiply = math..mul</syntaxhighlight>
this is the definition using lambda abstraction
this is the definition using lambda abstraction
<lang Ursala>multiply = ("a","b"). math..mul ("a","b")</lang>
<syntaxhighlight lang="ursala">multiply = ("a","b"). math..mul ("a","b")</syntaxhighlight>
and this is the definition using pattern matching.
and this is the definition using pattern matching.
<lang Ursala>multiply("a","b") = math..mul ("a","b")</lang>
<syntaxhighlight lang="ursala">multiply("a","b") = math..mul ("a","b")</syntaxhighlight>


=={{header|V}}==
=={{header|V}}==
V uses stack for input arguments and '.' is a word that takes a quote and binds the first word to the sequence of actions supplied in the quote.
V uses stack for input arguments and '.' is a word that takes a quote and binds the first word to the sequence of actions supplied in the quote.
<lang v>[multiply *].</lang>
<syntaxhighlight lang="v">[multiply *].</syntaxhighlight>
Using it
Using it
<lang v>2 3 multiply
<syntaxhighlight lang="v">2 3 multiply
=6</lang>
=6</syntaxhighlight>
V also allows internal bindings.
V also allows internal bindings.
<lang v>[multiply
<syntaxhighlight lang="v">[multiply
[a b] let
[a b] let
a b *].</lang>
a b *].</syntaxhighlight>


=={{header|VBA}}==
=={{header|VBA}}==
<lang vb>Function Multiply(lngMcand As Long, lngMplier As Long) As Long
<syntaxhighlight lang="vb">Function Multiply(lngMcand As Long, lngMplier As Long) As Long
Multiply = lngMcand * lngMplier
Multiply = lngMcand * lngMplier
End Function</lang>
End Function</syntaxhighlight>
To use this function :
To use this function :
<lang vb>Sub Main()
<syntaxhighlight lang="vb">Sub Main()
Dim Result As Long
Dim Result As Long
Result = Multiply(564231, 897)
Result = Multiply(564231, 897)
End Sub</lang>
End Sub</syntaxhighlight>


=={{header|VBScript}}==
=={{header|VBScript}}==
<lang vb>function multiply( multiplicand, multiplier )
<syntaxhighlight lang="vb">function multiply( multiplicand, multiplier )
multiply = multiplicand * multiplier
multiply = multiplicand * multiplier
end function</lang>
end function</syntaxhighlight>
Usage:
Usage:
<lang vb>dim twosquared
<syntaxhighlight lang="vb">dim twosquared
twosquared = multiply(2, 2)</lang>
twosquared = multiply(2, 2)</syntaxhighlight>


=={{header|Visual Basic}}==
=={{header|Visual Basic}}==
{{works with|Visual Basic|VB6 Standard}}
{{works with|Visual Basic|VB6 Standard}}
<syntaxhighlight lang="vb">
<lang vb>
Function multiply(a As Integer, b As Integer) As Integer
Function multiply(a As Integer, b As Integer) As Integer
multiply = a * b
multiply = a * b
End Function
End Function
</syntaxhighlight>
</lang>
Call the function
Call the function
<lang vb>Multiply(6, 111)</lang>
<syntaxhighlight lang="vb">Multiply(6, 111)</syntaxhighlight>


=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
<lang vbnet>Function Multiply(ByVal a As Integer, ByVal b As Integer) As Integer
<syntaxhighlight lang="vbnet">Function Multiply(ByVal a As Integer, ByVal b As Integer) As Integer
Return a * b
Return a * b
End Function</lang>
End Function</syntaxhighlight>
Call the function
Call the function
<lang vbnet>Multiply(1, 1)</lang>
<syntaxhighlight lang="vbnet">Multiply(1, 1)</syntaxhighlight>


=={{header|Wart}}==
=={{header|Wart}}==
A straightforward way to say how calls of the form <code>(multiply a b)</code> are translated:
A straightforward way to say how calls of the form <code>(multiply a b)</code> are translated:
<lang python>def (multiply a b)
<syntaxhighlight lang="python">def (multiply a b)
a*b</lang>
a*b</syntaxhighlight>
<lang python>(multiply 3 4)
<syntaxhighlight lang="python">(multiply 3 4)
=> 12</lang>
=> 12</syntaxhighlight>
Functions can also use keyword args.
Functions can also use keyword args.
<lang python>(multiply 3 :a 4) # arg order doesn't matter here, but try subtract instead
<syntaxhighlight lang="python">(multiply 3 :a 4) # arg order doesn't matter here, but try subtract instead
=> 12</lang>
=> 12</syntaxhighlight>
Finally, we can give parameters better keyword args using <em>aliases</em>:
Finally, we can give parameters better keyword args using <em>aliases</em>:
<lang python>def (multiply a b|by)
<syntaxhighlight lang="python">def (multiply a b|by)
(* a b)</lang>
(* a b)</syntaxhighlight>
<lang python>multiply 3 :by 4
<syntaxhighlight lang="python">multiply 3 :by 4
=> 12</lang>
=> 12</syntaxhighlight>


=={{header|WebAssembly}}==
=={{header|WebAssembly}}==
Line 3,245: Line 3,245:
The following 'multiply' function will work for any type(s) that support the '*' operator.
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.
However, it will produce a runtime error otherwise, as demonstrated by the final example.
<lang ecmascript>var multiply = Fn.new { |a, b| a * b }
<syntaxhighlight lang="ecmascript">var multiply = Fn.new { |a, b| a * b }


System.print(multiply.call(3, 7))
System.print(multiply.call(3, 7))
System.print(multiply.call("abc", 3))
System.print(multiply.call("abc", 3))
System.print(multiply.call([1], 5))
System.print(multiply.call([1], 5))
System.print(multiply.call(true, false))</lang>
System.print(multiply.call(true, false))</syntaxhighlight>


{{out}}
{{out}}
Line 3,276: Line 3,276:
The following is Unix-style "as" assembler syntax (including GNU as). The resulting function can be called from C with <code>multiply(123,456)</code>.
The following is Unix-style "as" assembler syntax (including GNU as). The resulting function can be called from C with <code>multiply(123,456)</code>.


<lang asm> .text
<syntaxhighlight lang="asm"> .text
.globl multiply
.globl multiply
.type multiply,@function
.type multiply,@function
Line 3,282: Line 3,282:
movl 4(%esp), %eax
movl 4(%esp), %eax
mull 8(%esp)
mull 8(%esp)
ret</lang>
ret</syntaxhighlight>


The <code>.type</code> directive is important for code which will go into a shared library. You can get away without it for a static link. It ensures the linker knows to dispatch calls from the mainline to the function via a PLT entry. (If omitted the code is copied at runtime into some mainline space. Without a <code>.size</code> directive only 4 bytes will be copied.)
The <code>.type</code> directive is important for code which will go into a shared library. You can get away without it for a static link. It ensures the linker knows to dispatch calls from the mainline to the function via a PLT entry. (If omitted the code is copied at runtime into some mainline space. Without a <code>.size</code> directive only 4 bytes will be copied.)
Line 3,288: Line 3,288:
===NASM===
===NASM===
{{works with|NASM}}
{{works with|NASM}}
<lang asm>section .text
<syntaxhighlight lang="asm">section .text
global _start
global _start
Line 3,311: Line 3,311:
push 6
push 6
push 16
push 16
call _multiply_stack</lang>
call _multiply_stack</syntaxhighlight>


===MASM===
===MASM===
However, in MASM we do have function statements due to the preprocessor.
However, in MASM we do have function statements due to the preprocessor.
{{works with|MASM}}
{{works with|MASM}}
<lang asm>multiply proc arg1:dword, arg2:dword
<syntaxhighlight lang="asm">multiply proc arg1:dword, arg2:dword
mov eax, arg1
mov eax, arg1
mov ebx, arg2
mov ebx, arg2
Line 3,322: Line 3,322:
mov eax, ebx
mov eax, ebx
ret
ret
multiply endp</lang>
multiply endp</syntaxhighlight>
Then to call it.
Then to call it.
<lang asm>invoke multiply, 6, 16
<syntaxhighlight lang="asm">invoke multiply, 6, 16
;or..
;or..
push 16
push 16
push 6
push 6
call multiply</lang>
call multiply</syntaxhighlight>
Return values are usually put into the register EAX. This, of course is not a must it's simply that it's somewhat of a unofficial standard. For example, C/C++ preprocessors/compilers will translate "return value" into "mov eax, value" followed by the return to caller instruction "ret".
Return values are usually put into the register EAX. This, of course is not a must it's simply that it's somewhat of a unofficial standard. For example, C/C++ preprocessors/compilers will translate "return value" into "mov eax, value" followed by the return to caller instruction "ret".


=={{header|XBS}}==
=={{header|XBS}}==
Functions are defined by using the '''func''' keyword.
Functions are defined by using the '''func''' keyword.
<lang XBS>func multiply(a,b){
<syntaxhighlight lang="xbs">func multiply(a,b){
send a*b;
send a*b;
}</lang>
}</syntaxhighlight>


=={{header|XLISP}}==
=={{header|XLISP}}==
Functions can be defined using either 'classic' Lisp syntax:
Functions can be defined using either 'classic' Lisp syntax:
<lang lisp>(defun multiply (x y)
<syntaxhighlight lang="lisp">(defun multiply (x y)
(* x y))</lang>
(* x y))</syntaxhighlight>
or Scheme-style syntax:
or Scheme-style syntax:
<lang scheme>(define (multiply x y)
<syntaxhighlight lang="scheme">(define (multiply x y)
(* x y))</lang>
(* x y))</syntaxhighlight>
or, if you prefer, with <tt>LAMBDA</tt>:
or, if you prefer, with <tt>LAMBDA</tt>:
<lang scheme>(define multiply
<syntaxhighlight lang="scheme">(define multiply
(lambda (x y) (* x y)))</lang>
(lambda (x y) (* x y)))</syntaxhighlight>


=={{header|Xojo}}==
=={{header|Xojo}}==
<lang vbnet>Function Multiply(ByVal a As Integer, ByVal b As Integer) As Integer
<syntaxhighlight lang="vbnet">Function Multiply(ByVal a As Integer, ByVal b As Integer) As Integer
Return a * b
Return a * b
End Function</lang>
End Function</syntaxhighlight>
Call the function
Call the function
<lang vbnet>Dim I As Integer = Multiply(7, 6)</lang>
<syntaxhighlight lang="vbnet">Dim I As Integer = Multiply(7, 6)</syntaxhighlight>


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>func Multiply(A, B); \the characters in parentheses are only a comment
<syntaxhighlight lang="xpl0">func Multiply(A, B); \the characters in parentheses are only a comment
int A, B; \the arguments are actually declared here, as integers
int A, B; \the arguments are actually declared here, as integers
return A*B; \the default (undeclared) function type is integer
return A*B; \the default (undeclared) function type is integer
Line 3,363: Line 3,363:
func real FloatMul(A, B); \floating point version
func real FloatMul(A, B); \floating point version
real A, B; \arguments are declared here as floating point (doubles)
real A, B; \arguments are declared here as floating point (doubles)
return A*B;</lang>
return A*B;</syntaxhighlight>


=={{header|XSLT}}==
=={{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).
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="multiply">
<syntaxhighlight lang="xslt"><xsl:template name="multiply">
<xsl:param name="a" select="2"/>
<xsl:param name="a" select="2"/>
<xsl:param name="b" select="3"/>
<xsl:param name="b" select="3"/>
<xsl:value-of select="$a * $b"/>
<xsl:value-of select="$a * $b"/>
</xsl:template></lang>
</xsl:template></syntaxhighlight>
Usage examples.
Usage examples.
<lang xslt><xsl:call-template name="multiply">
<syntaxhighlight lang="xslt"><xsl:call-template name="multiply">
<xsl:with-param name="a">4</xsl:with-param>
<xsl:with-param name="a">4</xsl:with-param>
<xsl:with-param name="b">5</xsl:with-param>
<xsl:with-param name="b">5</xsl:with-param>
</xsl:call-template>
</xsl:call-template>
<xsl:call-template name="multiply"/> <-- using default parameters of 2 and 3 --></lang>
<xsl:call-template name="multiply"/> <-- using default parameters of 2 and 3 --></syntaxhighlight>


Available in XSLT 2.0 and later versions.
Available in XSLT 2.0 and later versions.
<lang xslt><xsl:function name="mf:multiply">
<syntaxhighlight lang="xslt"><xsl:function name="mf:multiply">
<xsl:param name="a"/>
<xsl:param name="a"/>
<xsl:param name="b"/>
<xsl:param name="b"/>
<xsl:value-of select="$a * $b"/>
<xsl:value-of select="$a * $b"/>
</xsl:function></lang>
</xsl:function></syntaxhighlight>
Usage examples.
Usage examples.
<lang xslt>{mf:multiply(2,3)}
<syntaxhighlight lang="xslt">{mf:multiply(2,3)}
<xsl:value-of select="mf:multiply(2,3)" /></lang>
<xsl:value-of select="mf:multiply(2,3)" /></syntaxhighlight>


=={{header|Yorick}}==
=={{header|Yorick}}==
<lang yorick>func multiply(x, y) {
<syntaxhighlight lang="yorick">func multiply(x, y) {
return x * y;
return x * y;
}</lang>
}</syntaxhighlight>
Example of interactive usage:
Example of interactive usage:
<pre>> multiply(2, 4.5)
<pre>> multiply(2, 4.5)
Line 3,401: Line 3,401:
A function's return values are whatever registers or memory are changed by the function. A good programmer will explain what is returned where by using comments.
A function's return values are whatever registers or memory are changed by the function. A good programmer will explain what is returned where by using comments.


<lang z80>doMultiply:
<syntaxhighlight lang="z80">doMultiply:
;returns HL = HL times A. No overflow protection.
;returns HL = HL times A. No overflow protection.
push bc
push bc
Line 3,427: Line 3,427:
pop de
pop de
pop bc
pop bc
ret</lang>
ret</syntaxhighlight>


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>fcn multiply(x,y){x*y}</lang>
<syntaxhighlight lang="zkl">fcn multiply(x,y){x*y}</syntaxhighlight>
<lang zkl>fcn(x,y){x*y}(4.5,3) // --> 13.5</lang>
<syntaxhighlight lang="zkl">fcn(x,y){x*y}(4.5,3) // --> 13.5</syntaxhighlight>
Since all functions are vararg:<lang zkl>fcn multiply{vm.arglist.reduce('*)}
Since all functions are vararg:<syntaxhighlight lang="zkl">fcn multiply{vm.arglist.reduce('*)}
multiply(1,2,3,4,5) //--> 120</lang>
multiply(1,2,3,4,5) //--> 120</syntaxhighlight>
Operators are first class objects so:<lang zkl>var mul=Op("*"); mul(4,5) //-->20</lang>
Operators are first class objects so:<syntaxhighlight lang="zkl">var mul=Op("*"); mul(4,5) //-->20</syntaxhighlight>


=={{header|ZX Spectrum Basic}}==
=={{header|ZX Spectrum Basic}}==
On the ZX Spectrum, function names are limited to one letter. Note that the function becomes effective as soon as it is entered into the program, and does not need to be run
On the ZX Spectrum, function names are limited to one letter. Note that the function becomes effective as soon as it is entered into the program, and does not need to be run
<lang zxbasic>10 PRINT FN m(3,4): REM call our function to produce a value of 12
<syntaxhighlight lang="zxbasic">10 PRINT FN m(3,4): REM call our function to produce a value of 12
20 STOP
20 STOP
9950 DEF FN m(a,b)=a*b</lang>
9950 DEF FN m(a,b)=a*b</syntaxhighlight>
{{omit from|GUISS}}
{{omit from|GUISS}}