Function definition: Difference between revisions

(43 intermediate revisions by 27 users not shown)
Line 17:
=={{header|11l}}==
Function definition:
<langsyntaxhighlight lang="11l">F multiply(a, b)
R a * b</langsyntaxhighlight>
Lambda function definition:
<langsyntaxhighlight lang="11l">V multiply = (a, b) -> a * b</langsyntaxhighlight>
 
=={{header|360 Assembly}}==
Linkage conventions are: register 1 : the parameter list, register 0 : the return value,
and register 14 : the return address.
<langsyntaxhighlight lang="360asm">DEFFUN CSECT
USING DEFFUN,R13
SAVEAREA B PROLOG-SAVEAREA(R15)
Line 62:
Z DS F
YREGS
END DEFFUN</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="asm6502">MULTIPLY: STX MULN ; 6502 has no "acc += xreg" instruction,
TXA ; so use a memory address
MULLOOP: DEY
Line 73:
CPY #$01
BNE MULLOOP
RTS</langsyntaxhighlight>
An alternative implementation that multiplies A by X and checks if A/X is zero.
<langsyntaxhighlight lang="asm6502">; https://skilldrick.github.io/easy6502/
; Multiplies A by X
 
Line 96:
MAIN: LDA #50
LDX #5
JSR MULTIPLY</langsyntaxhighlight>
 
=={{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.
 
<langsyntaxhighlight lang="68000devpac">MOVE.L D0,#$0200
MOVE.L D1,#$0400
 
Line 113:
MULU D0,D1
RTS
</syntaxhighlight>
</lang>
 
 
=={{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.
<langsyntaxhighlight lang="asm">ORG RESET
mov a, #100
mov b, #10
Line 129:
multiply:
mul ab
ret</langsyntaxhighlight>
 
=={{header|8086 Assembly}}==
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.
 
<langsyntaxhighlight lang="asm">start:
mov al, 0x04
mov bl, 0x05
call domultiplymultiply
;at this point in execution, the AX register contains 0x0900.
;more code goes here, ideally with some sort of guard against "fallthrough" into summultiply.
 
; somewhere far away from start
multiply:
sum:
mul bl ;outputs 0x0014 to ax
ret</langsyntaxhighlight>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program functMul64.s */
Line 206:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
 
=={{header|ACL2}}==
<langsyntaxhighlight Lisplang="lisp">(defun multiply (a b) (* a b))</langsyntaxhighlight>
 
=={{header|ActionScript}}==
<langsyntaxhighlight lang="actionscript">function multiply(a:Number, b:Number):Number {
return a * b;
}</langsyntaxhighlight>
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">function Multiply (A, B : Float) return Float;</langsyntaxhighlight>
and an implementation of:
<langsyntaxhighlight lang="ada">function Multiply (A, B : Float) return Float is
begin
return A * B;
end Multiply;</langsyntaxhighlight>
 
 
The Ada 2012 standard provides an even simpler way to define and implement functions:
 
<langsyntaxhighlight Adalang="ada">function Multiply(A, B: Float) return Float is (A * B);</langsyntaxhighlight>
 
 
Ada supports generic functions which can take generic formal parameters like the numeric type to use:
<langsyntaxhighlight lang="ada">generic
type Number is digits <>;
function Multiply (A, B : Number) return Number;</langsyntaxhighlight>
implemented as:
<langsyntaxhighlight lang="ada">function Multiply (A, B : Number) return Number is
begin
return A * B;
end Multiply;</langsyntaxhighlight>
To use this, you need to instantiate the function for each type e.g.
<langsyntaxhighlight lang="ada">
with Multiply;
...
Line 248:
type My_Integer is Range -100..100;
function Multiply_My_Integer is new Multiply(My_Integer);
</syntaxhighlight>
</lang>
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">real
multiply(real a, real b)
{
return a * b;
}</langsyntaxhighlight>
 
=={{header|ALGOL 60}}==
Line 277:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">PROC multiply = ( LONG REAL a, b ) LONG REAL:
(
a * b
)</langsyntaxhighlight>
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">long real procedure multiply( long real value a, b );
begin
a * b
end</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="algol">INTEGER FUNCTION MULTIPLY( A, B );
INTEGER A, B;
BEGIN
MULTIPLY := A * B;
END;</langsyntaxhighlight>
 
=={{header|Amazing Hopper}}==
Hopper has no functions, but they can be declared with macros, which are resolved at compile time. Access to the working stack is global, but "local" variables can be declared in program segments written after the ".locals" clause. Let's look at some examples of declaring "functions".
<syntaxhighlight lang="c">
/* this need data into stack */
#context Multiplication
mul
Return \\
#synon Multiplication *getproduct
 
#context-free anothermul
/* #defn Args(*) #GENCODE $$$*$$$ #REVLIST=0,mov(#REVLIST);#ENDGEN, */
Args 'a,b'
Return ( #(a*b) )\\
#synon anothermul *getanotherproduct
 
#include <jambo.h>
 
#prototype _multiply(_X_,_Y_)
#synon __multiply Multiply
 
Main
/* "prototipos" of functions and procedures.
Solves internaly */
Printnl ( Multiply ( 10, 4 ) )
Printnl ( __multiply ( 10, 4 ) )
/* definición alternativa 1 */
Printnl ( Set' 10,4 ', Gosub ' Multiply2 ')
/* aseembler Hopper 1 */
{10,4} jsub( Multiply3 ), {"\n"} print
/* assembler Hopper 2 */
{10,4} jsub( Multiply4 ), {"\n"} print
/* context */
Set '10,4', now get product, and print with newline
/* context-free */
Set '10,4', and get another product; then print with newline
End
 
.locals /* Subrutines */
 
_multiply(a,b)
Return ( Mul(a,b) )
 
/* Define is macro. Others macros: Function, Procedure:
#defn Define(_F_,*) _F_:,#GENCODE $$$*$$$ #REVLIST=0;mov(#REVLIST);#ENDGEN;
#defn Function(_F_,*) _F_:,#GENCODE $$$*$$$ #REVLIST=0;mov(#REVLIST);#ENDGEN;
#defn Procedure(_F_,*) _F_:,#GENCODE $$$*$$$ #REVLIST=0;mov(#REVLIST);#ENDGEN;
*/
Define 'Multiply2, a,b'
Return ( Mul(a,b) )
 
Multiply3:
b=0, mov(b), a=0, mov(a)
{a,b}mul /* result into stack */
Return
 
Multiply4:
mul /* get values from stack,
and put result into stack */
back /* Return */
</syntaxhighlight>
{{out}}
<pre>
40.000000
40.000000
40.000000
40.000000
40.000000
40.000000
40.000000
</pre>
 
=={{header|AmigaE}}==
<langsyntaxhighlight lang="amigae">PROC my_molt(a,b)
-> other statements if needed... here they are not
ENDPROC a*b -> return value
Line 307 ⟶ 384:
PROC main()
WriteF('\d\n', my_molt(10,20))
ENDPROC</langsyntaxhighlight>
 
=={{header|AntLang}}==
<langsyntaxhighlight AntLanglang="antlang">multiply: * /`*' is a normal function
multiply: {x * y}</langsyntaxhighlight>
Explicit definition has the syntax:
<langsyntaxhighlight AntLanglang="antlang">{expr-or-def1; expr-or-def2; ..; return-expr}</langsyntaxhighlight>
Inside functions, the variable args contains the sequence of arguments.
x, y and z contain the first, second and third argument.
Line 319 ⟶ 396:
=={{header|APL}}==
{{works with|GNU_APL}}
<langsyntaxhighlight lang="apl">
⍝⍝ APL2 'tradfn' (traditional function)
⍝⍝ This syntax works in all dialects including GNU APL and Dyalog.
Line 328 ⟶ 405:
⍝⍝ A 'dfn' or 'lambda' (anonymous function)
multiply ← {⍺×⍵}
</syntaxhighlight>
</lang>
 
{{works with|Dyalog_APL}}
<langsyntaxhighlight lang="apl">
⍝⍝ Dyalog dfn (lambda) syntax
multiply ← ×
</syntaxhighlight>
</lang>
Works on arrays of any rank (any number of dimensions): atoms, lists, tables, etc.
 
=={{header|AppleScript}}==
<langsyntaxhighlight AppleScriptlang="applescript">to multiply(a as number, b as number)
return a * b
end</langsyntaxhighlight>
 
A function in AppleScript is called a "handler". It can take one of three different forms, depending on what the scripter finds most convenient. Calls to it must match the form used in the handler definition. The above is an example of a handler with "positional" parameters. Either <code>to</code> or <code>on</code> may be used as the first word in the headerhandler linedefinition. When the script's is compiled, the handler label is automatically appended to the <code>end</code> line too if it wasn't written in.
 
Handler names followed by zero or more parameters within parentheses are called "positional" -- the number and order of the parameters in the caller must match those in the handler definition.
<lang applescript>on multiply(a, b)
 
<syntaxhighlight lang="applescript">on multiply(a, b)
return a * b
end multiply
 
multiply(2, 3)</langsyntaxhighlight>
 
AppleScript also offers handlers with "labeledprepositional" [sic]labeled parameters. These aren't used muchoften now asbecause the limited choiceset of labelAppleScript-defined enumsprepositions makes it difficult to choose ones that make sense in English, although it's just about possible. here:
 
These prepositions can be used: <code>about, above, against, apart from, around, aside from, at, below, beneath, beside, between, by, for, from, instead of, into, on, onto, out of, over, since, thru, through, and under</code>. Also, <code>of</code> is available, but if used it must be the first parameter.
<lang applescript>on multiplication of a by b
 
Example:
 
<syntaxhighlight lang="applescript">on multiplication of a by b
return a * b
end multiplication
 
multiplication of 2 by 3 -- Or: (multiplication by 3) of 2, or: 2's (multiplication by 3)</langsyntaxhighlight>
 
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 ⟶ 445:
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:
 
<langsyntaxhighlight lang="applescript">on multiply:a |by|:b -- 'by' is "barred" here because otherwise it's a reserved word.
return a * b
end multiply:|by|:
 
my multiply:2 |by|:3</langsyntaxhighlight>
 
=={{header|Applesoft BASIC}}==
Applesoft BASIC functions are unary meaning they only take one argument. As the task asks for a multiply function which takes two arguments this poses a problem. To get around this, the multiply function MU takes one argument as the offset into an array of parameters.
 
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)
20 P(1) = 611 : P(2) = 78 : PRINT FN MULTIPLY(1)</lang>
 
<lang basic>47658</lang>
 
=={{header|Argile}}==
<langsyntaxhighlight Argilelang="argile">use std
.: multiply <real a, real b> :. -> real {a * b}</langsyntaxhighlight>
with a macro and a variable number of parameters:
<langsyntaxhighlight Argilelang="argile">use std
=: multiply <real a> [<real b>...] := -> real {Cgen a (@@1 (Cgen " * " b))}</langsyntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program functMul.s */
Line 525 ⟶ 598:
 
 
</syntaxhighlight>
</lang>
 
=={{header|ArnoldC}}==
<langsyntaxhighlight 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 b
Line 539 ⟶ 612:
ENOUGH TALK
I'LL BE BACK product
HASTA LA VISTA, BABY</langsyntaxhighlight>
 
=={{header|Arturo}}==
<langsyntaxhighlight lang="arturo">multiply: $[x,y][x*y]
 
print multiply 3 7
Line 551 ⟶ 624:
 
print multiply2 3 7
</syntaxhighlight>
</lang>
 
{{out}}
Line 559 ⟶ 632:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">MsgBox % multiply(10,2)
 
multiply(multiplicand, multiplier) {
Return (multiplicand * multiplier)
}</langsyntaxhighlight>
 
=={{header|AutoIt}}==
<langsyntaxhighlight AutoItlang="autoit">#AutoIt Version: 3.2.10.0
$I=11
$J=12
Line 572 ⟶ 645:
Func product($a,$b)
Return $a * $b
EndFunc</langsyntaxhighlight>
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk">function multiply(a, b)
{
return a*b
Line 581 ⟶ 654:
BEGIN {
print multiply(5, 6)
}</langsyntaxhighlight>
 
=={{header|Axe}}==
<langsyntaxhighlight lang="axe">Lbl MULT
r₁*r₂
Return</langsyntaxhighlight>
 
=={{header|BASIC}}==
==={{header|ANSI BASIC}}===
{{works with|QBasic}}
In ANSI BASIC, functions can be defined as either formulas or multi-line external or internal subroutines. External functions are independent program units that can be called from within the program. Internal functions are considered part of the program unit they are contained in and can only be called from within that unit. External functions do not share any information with other program units and exchange information through parameters and returned values. Internal functions share everything with their surrounding program unit except for their parameters. Internal functions do not have local variables.
<lang qbasic>DECLARE FUNCTION multiply% (a AS INTEGER, b AS INTEGER)
{{works with|Decimal BASIC}}
<syntaxhighlight lang="basic">
100 DEF Multiply(A, B) = A * B
110 DECLARE FUNCTION MultiplyI
120 DECLARE EXTERNAL FUNCTION MultiplyE
130 PRINT Multiply(3, 1.23456)
140 PRINT MultiplyI(3, 1.23456)
150 PRINT MultiplyE(3, 1.23456)
160 FUNCTION MultiplyI(X, Y)
170 LET MultiplyI = X * Y
180 END FUNCTION
190 END
200 EXTERNAL FUNCTION MultiplyE(A, B)
210 LET MultiplyE = A * B
220 END FUNCTION
</syntaxhighlight>
{{out}}
<pre>
3.70368
3.70368
3.70368
</pre>
 
==={{header|Applesoft BASIC}}===
FUNCTION multiply% (a AS INTEGER, b AS INTEGER)
Applesoft BASIC functions are unary meaning they only take one argument. As the task asks for a multiply function which takes two arguments this poses a problem. To get around this, the multiply function MU takes one argument as the offset into an array of parameters.
multiply = a * b
 
END FUNCTION</lang>
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.
 
<syntaxhighlight lang="basic">10 DEF FN MULTIPLY(P) = P(P) * P(P+1)
20 P(1) = 611 : P(2) = 78 : PRINT FN MULTIPLY(1)</syntaxhighlight>
 
<syntaxhighlight lang="basic">47658</syntaxhighlight>
 
==={{header|BASIC256}}===
<langsyntaxhighlight lang="freebasic">function multiply(a, b)
return a * b
end function</langsyntaxhighlight>
 
==={{header|BBC BASIC}}===
BBC BASIC supports both single-line and multi-line function definitions. Note that the function name ''must'' begin with '''FN'''.
 
Single-line function:
<syntaxhighlight lang="bbcbasic">PRINT FNmultiply(6,7)
END
 
DEF FNmultiply(a,b) = a * b</syntaxhighlight>
Multiline function:
<syntaxhighlight lang="bbcbasic">DEF FNmultiply(a,b)
LOCAL c
c = a * b
= c</syntaxhighlight>
 
=== {{header|Chipmunk Basic}} ===
<syntaxhighlight lang="basic">
10 rem Function definition
 
20 rem ** 1. Function defined as formula. An obsolete way - does not work properly with integer formal parameters (e.g. x%).
30 def fnmultiply(a, b) = a * b
 
40 rem ** Call the functions
50 print multiply(3,1.23456)
60 print fn multiply(3,1.23456)
70 end
 
200 rem ** 2. Function defined as subroutine returning a value
210 sub multiply(a,b)
220 multiply = a*b
230 end sub
</syntaxhighlight>
{{out}}
<pre>
3.70368
3.70368
</pre>
 
==={{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.
<langsyntaxhighlight lang="basic">10 DEF FN MULT(X) = X*Y
20 Y = 4 : REM VALUE OF SECOND ARGUMENT MUST BE ASSIGNED SEPARATELY
30 PRINT FN MULT(3)</langsyntaxhighlight>
 
==={{header|IS-BASICCreative Basic}}===
<syntaxhighlight lang="creative basic">
<lang IS-BASIC>100 DEF MULTIPLY(A,B)=A*B</lang>
DECLARE Multiply(N1:INT,N2:INT)
 
DEF A,B:INT
 
A=2:B=2
 
OPENCONSOLE
 
PRINT Multiply(A,B)
 
PRINT:PRINT"Press any key to close."
 
DO:UNTIL INKEY$<>""
 
CLOSECONSOLE
 
END
 
SUB Multiply(N1:INT,N2:INT)
 
DEF Product:INT
 
Product=N1*N2
 
RETURN Product
 
'Can also be written with no code in the subroutine and just RETURN N1*N2.
</syntaxhighlight>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Function multiply(d1 As Double, d2 As Double) As Double
Return d1 * d2
End Function</syntaxhighlight>
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).
 
Alternatively, one could write a macro though this wouldn't be type-safe:
 
<syntaxhighlight lang="freebasic">#Define multiply(d1, d2) (d1) * (d2)</syntaxhighlight>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebasic">window 1
 
local fn multiply( a as long, b as long ) as long
end fn = a * b
 
print fn multiply( 3, 9 )
 
HandleEvents</syntaxhighlight>
Output:
<pre>
27
</pre>
 
==={{header|Gambas}}===
'''[https://gambas-playground.proko.eu/?gist=bc93236474d9937217dd4117026f7441 Click this link to run this code]'''
<syntaxhighlight lang="gambas">Public Sub Main()
 
Print Multiply(56, 4.66)
 
End
 
Public Sub Multiply(f1 As Float, f2 As Float) As Float
 
Return f1 * f2
 
End</syntaxhighlight>
Output:
<pre>
260.96
</pre>
 
==={{header|GWBASICGW-BASIC}}===
{{works with|BASICA}}
<langsyntaxhighlight BASIClang="basic">10 DEF FNMULT(X,Y)=X*Y
20 PRINT FNMULT(5,6)
39 END
</syntaxhighlight>
</lang>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 DEF MULTIPLY(A,B)=A*B</syntaxhighlight>
 
==={{header|IWBASIC}}===
<syntaxhighlight lang="iwbasic">
'1. Not Object Oriented Program
 
DECLARE Multiply(N1:INT,N2:INT),INT
DEF A,B:INT
A=2:B=2
OPENCONSOLE
PRINT Multiply(A,B)
 
PRINT
'When compiled as a console only program, a press any key to continue is automatic.
CLOSECONSOLE
END
SUB Multiply(N1:INT,N2:INT),INT
DEF Product:INT
Product=N1*N2
RETURN Product
ENDSUB
 
'Can also be written with no code in the subroutine and just RETURN N1*N2.
 
----
 
'2. Not Object Oriented Program Using A Macro
 
$MACRO Multiply (N1,N2) (N1*N2)
 
DEF A,B:INT
 
A=5:B=5
 
OPENCONSOLE
 
PRINT Multiply (A,B)
 
PRINT
 
'When compiled as a console only program, a press any key to continue is automatic.
CLOSECONSOLE
 
END
 
----
 
'3. In An Object Oriented Program
 
CLASS Associate
'functions/methods
DECLARE Associate:'object constructor
DECLARE _Associate:'object destructor
'***Multiply declared***
DECLARE Multiply(UnitsSold:UINT),UINT
'members
DEF m_Price:UINT
DEF m_UnitsSold:UINT
DEF m_SalesTotal:UINT
ENDCLASS
 
DEF Emp:Associate
 
m_UnitsSold=10
 
Ass.Multiply(m_UnitsSold)
 
OPENCONSOLE
 
PRINT"Sales total: ",:PRINT"$"+LTRIM$(STR$(Emp.m_SalesTotal))
 
PRINT
 
CLOSECONSOLE
 
END
 
'm_price is set in constructor
SUB Associate::Multiply(UnitsSold:UINT),UINT
m_SalesTotal=m_Price*UnitsSold
RETURN m_SalesTotal
ENDSUB
 
SUB Associate::Associate()
m_Price=10
ENDSUB
 
SUB Associate::_Associate()
'Nothing to cleanup
ENDSUB
</syntaxhighlight>
 
==={{header|Liberty BASIC}}===
{{works with|Just BASIC}}
<syntaxhighlight lang="lb">' define & call a function
 
print multiply( 3, 1.23456)
 
wait
 
function multiply( m1, m2)
multiply =m1 *m2
end function
 
end</syntaxhighlight>
 
==={{header|Locomotive Basic}}===
<syntaxhighlight lang="locobasic">10 DEF FNmultiply(x,y)=x*y
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.
 
==={{header|OxygenBasic}}===
<syntaxhighlight lang="text">
'SHORT FORMS:
float multiply(float a,b) = a * b
Line 635 ⟶ 968:
'TEST:
print multiply(pi,2) '6.28...
</syntaxhighlight>
</lang>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">Procedure multiply(a,b)
ProcedureReturn a*b
EndProcedure</syntaxhighlight>
 
==={{header|QBasic}}===
<langsyntaxhighlight lang="qbasic">'This function could either be used for all numeric types
'(as they are implicitly convertible to Double)
FUNCTION multiply# (a AS DOUBLE, b AS DOUBLE)
Line 649 ⟶ 987:
 
PRINT multiply(3, 1.23456)
PRINT FNmultiply#(3, 1.23456)</langsyntaxhighlight>
{{out}}
<pre> 3.703680038452148</pre>
 
==={{header|QuickBASIC}}===
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">DECLARE FUNCTION multiply% (a AS INTEGER, b AS INTEGER)
 
FUNCTION multiply% (a AS INTEGER, b AS INTEGER)
multiply = a * b
END FUNCTION</syntaxhighlight>
 
==={{header|REALbasic}}===
<syntaxhighlight lang="vb">
Function Multiply(a As Integer, b As Integer) As Integer
Return a * b
End Function
</syntaxhighlight>
 
==={{header|S-BASIC}}===
S-BASIC is unusual in that the function return value is assigned to the END statement that terminates the function.
<syntaxhighlight lang="basic">
function multiply(a, b = integer) = integer
end = a * b
 
rem - exercise the function
 
print "The product of 9 times 3 is"; multiply(9, 3)
 
end
</syntaxhighlight>
{{out}}
<pre>
The product of 9 times 3 is 27
</pre>
 
==={{header|True BASIC}}===
The <code>FUNCTION</code> and <code>DEF</code> commands are synonymous and can be interchanged.
<langsyntaxhighlight lang="qbasic">FUNCTION multiply(a, b)
LET multiply = a * b
END FUNCTION
Line 663 ⟶ 1,033:
DEF multiply (a, b) = a * b
 
END</langsyntaxhighlight>
 
==={{header|TI-89 BASIC}}===
<syntaxhighlight lang="ti89b">multiply(a, b)
Func
Return a * b
EndFunc</syntaxhighlight>
 
==={{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.
<lang>Print FUNC(_multiply (23, 65))
<syntaxhighlight lang="text">Print FUNC(_multiply (23, 65))
End
 
_multiply Param (2) : Return (a@ * b@)</langsyntaxhighlight>
 
==={{header|VBA}}===
<syntaxhighlight lang="vb">Function Multiply(lngMcand As Long, lngMplier As Long) As Long
Multiply = lngMcand * lngMplier
End Function</syntaxhighlight>
To use this function :
<syntaxhighlight lang="vb">Sub Main()
Dim Result As Long
Result = Multiply(564231, 897)
End Sub</syntaxhighlight>
 
==={{header|VBScript}}===
<syntaxhighlight lang="vb">function multiply( multiplicand, multiplier )
multiply = multiplicand * multiplier
end function</syntaxhighlight>
Usage:
<syntaxhighlight lang="vb">dim twosquared
twosquared = multiply(2, 2)</syntaxhighlight>
 
==={{header|Visual Basic}}===
{{works with|Visual Basic|VB6 Standard}}
<syntaxhighlight lang="vb">
Function multiply(a As Integer, b As Integer) As Integer
multiply = a * b
End Function
</syntaxhighlight>
Call the function
<syntaxhighlight lang="vb">Multiply(6, 111)</syntaxhighlight>
 
==={{header|Visual Basic .NET}}===
<syntaxhighlight lang="vbnet">Function Multiply(ByVal a As Integer, ByVal b As Integer) As Integer
Return a * b
End Function</syntaxhighlight>
Call the function
<syntaxhighlight lang="vbnet">Multiply(1, 1)</syntaxhighlight>
 
==={{header|Yabasic}}===
<langsyntaxhighlight lang="yabasic">sub multiply(a, b)
return a * b
end sub</langsyntaxhighlight>
 
==={{header|Xojo}}===
<syntaxhighlight lang="vbnet">Function Multiply(ByVal a As Integer, ByVal b As Integer) As Integer
Return a * b
End Function</syntaxhighlight>
Call the function
<syntaxhighlight lang="vbnet">Dim I As Integer = Multiply(7, 6)</syntaxhighlight>
 
==={{header|ZX Spectrum Basic}}===
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
<syntaxhighlight lang="zxbasic">10 PRINT FN m(3,4): REM call our function to produce a value of 12
20 STOP
9950 DEF FN m(a,b)=a*b</syntaxhighlight>
 
=={{header|Batch File}}==
Windows batch files only have procedures, not functions. Instead, environmental variables can be used as a global shared state.
<syntaxhighlight lang="text">@ECHO OFF
SET /A result = 0
CALL :multiply 2 3
Line 688 ⟶ 1,113:
GOTO :eof
 
:eof</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
BBC BASIC supports both single-line and multi-line function definitions. Note that the function name ''must'' begin with '''FN'''.
 
Single-line function:
<lang bbcbasic>PRINT FNmultiply(6,7)
END
 
DEF FNmultiply(a,b) = a * b</lang>
Multiline function:
<lang bbcbasic>DEF FNmultiply(a,b)
LOCAL c
c = a * b
= c</lang>
 
=={{header|bc}}==
{{Works with|GNU bc}}
<langsyntaxhighlight lang="bc">define multiply(a, b) { return a*b }
 
print multiply(2, 3)</langsyntaxhighlight>
 
=={{header|BCPL}}==
A function is simply defined as an expression in terms of its arguments.
<langsyntaxhighlight lang="bcpl">let multiply(a, b) = a * b</langsyntaxhighlight>
 
Defining a block of code that executes some statements and then returns a
Line 719 ⟶ 1,130:
to define a function containing imperative statements. When used this way,
it is equivalent to the functions in most other imperative languages.
<langsyntaxhighlight lang="bcpl">let multiply(a, b) = valof
$( // any imperative statements could go here
resultis a * b
$)</langsyntaxhighlight>
 
=={{header|BlitzMax}}==
<langsyntaxhighlight lang="blitzmax">function multiply:float( a:float, b:float )
return a*b
end function
 
print multiply(3.1416, 1.6180)</langsyntaxhighlight>
 
{{out}}<pre>5.08310890</pre>
 
=={{header|Boo}}==
<langsyntaxhighlight lang="boo">def multiply(x as int, y as int):
return x * y
 
print multiply(3, 2)</langsyntaxhighlight>
 
=={{header|Binary Lambda Calculus}}==
 
In lambda calculus, multiplication on Church numerals is <code>mul = \m \n \f. m (n f)</code> which in BLC is
 
<pre>00 00 00 01 1110 01 110 10</pre>
 
If mul is used several times within an expression E, then they can share the same definition by using <code>(\mul. E)(\m\n\f. m (n f))</code>. For example, the cube function is <code>\n. (\mul. mul n (mul n n)) (\m\n\f. m (n f))</code> which in BLC is
 
<pre>00 01 00 01 01 10 110 01 01 10 110 110 0000000111100111010</pre>
 
=={{header|BQN}}==
Tacit definition:
<syntaxhighlight lang ="bqn">Multiply ← ×</langsyntaxhighlight>
 
With names:
<syntaxhighlight lang ="bqn">Multiply ← {𝕨×𝕩}</langsyntaxhighlight>
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat">multiply=a b.!arg:(?a.?b)&!a*!b;
out$multiply$(123456789.987654321); { writes 121932631112635269 to standard output }</langsyntaxhighlight>
 
=={{header|Brat}}==
<langsyntaxhighlight lang="brat">multiply = { x, y | x * y }
 
p multiply 3 14 #Prints 42</langsyntaxhighlight>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">double multiply(double a, double b)
{
return a * b;
}</langsyntaxhighlight>
===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).
<langsyntaxhighlight lang="c">#define MULTIPLY(X, Y) ((X) * (Y))</langsyntaxhighlight>
Parentheses should be added around parameters in the function definition to avoid order of operations errors when someone uses the macro as such:
<langsyntaxhighlight lang="c">x = MULTIPLY(x + z, y);</langsyntaxhighlight>
A program with that call would be compiled as if this were coded instead:
<langsyntaxhighlight lang="c">x = ((x + z) * (y));</langsyntaxhighlight>
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#}}==
<langsyntaxhighlight lang="csharp">static double multiply(double a, double b)
{
return a * b;
}</langsyntaxhighlight>
Anonymous function:
<langsyntaxhighlight lang="csharp">Func<double, double, double> multiply = ((a,b) => a*b);</langsyntaxhighlight>
 
=={{header|C++}}==
Line 781 ⟶ 1,202:
 
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:
<langsyntaxhighlight lang="cpp">inline double multiply(double a, double b)
{
return a*b;
}</langsyntaxhighlight>
If not only doubles, but numbers of arbitrary types are to be multiplied, a function template can be used:
<langsyntaxhighlight lang="cpp">template<typename Number>
Number multiply(Number a, Number b)
{
return a*b;
}</langsyntaxhighlight>
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>:
<langsyntaxhighlight lang="cpp">auto multiply(auto a, auto b)
{
return a*b;
}</langsyntaxhighlight>
 
=={{header|ChucK}}==
<syntaxhighlight lang="text">
fun float multiply (float a, float b)
{
Line 807 ⟶ 1,228:
// uncomment next line and change values to test
//<<< multiply(16,4) >>>;
</syntaxhighlight>
</lang>
 
=={{header|Clay}}==
<langsyntaxhighlight Claylang="clay">multiply(x,y) = x * y;</langsyntaxhighlight>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="lisp">(defn multiply [x y]
(* x y))
 
(multiply 4 5)</langsyntaxhighlight>
Or with multiple arities (in the manner of the actual <tt>*</tt> function):
<langsyntaxhighlight lang="lisp">(defn multiply
([] 1)
([x] x)
Line 825 ⟶ 1,246:
(reduce * (* x y) more)))
 
(multiply 2 3 4 5) ; 120</langsyntaxhighlight>
 
=={{header|CLU}}==
The following is a function that multiplies two integers and ignores any error conditions
(as most examples do).
<langsyntaxhighlight lang="clu">multiply = proc (a, b: int) returns (int)
return(a * b)
end multiply</langsyntaxhighlight>
 
The following is a type-parameterized function that wraps the built-in multiplication operator
Line 838 ⟶ 1,259:
It also shows the complete syntax of a function definition (type parameterization,
signals, and a <code>where</code> clause).
<langsyntaxhighlight lang="clu">multiply = proc [T: type] (a, b: T) returns (T)
signals (overflow, underflow)
where T has mul: proctype (T, T) returns (T)
signals (overflow, underflow)
return(a * b) resignal overflow, underflow
end multiply</langsyntaxhighlight>
 
=={{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:
{{worksWorks with|OpenCOBOLCOBOL-85}}
The following uses a subprogram:
<lang COBOL> IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. myTest.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 x PICPICTURE IS 9(3) VALUE IS 3.
01 y PICPICTURE IS 9(3) VALUE IS 2.
01 z PICPICTURE IS 9(9).
PROCEDURE DIVISION.
CALL "myMultiply" USING
Line 867 ⟶ 1,289:
DATA DIVISION.
LINKAGE SECTION.
01 x PICPICTURE IS 9(3).
01 y PICPICTURE IS 9(3).
01 z PICPICTURE IS 9(9).
PROCEDURE DIVISION USING BY REFERENCE x, y, z.
MULTIPLY x BY y GIVING z.
EXIT PROGRAM.
END PROGRAM myMultiply.</langsyntaxhighlight>
 
{{Works with|COBOL 2002}}
This example uses user-defined functions, which were added in COBOL 2002.
This example uses user-defined functions.
{{works with|GNU Cobol|2.0}}
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. myTest.
ENVIRONMENT DIVISION.
Line 885 ⟶ 1,307:
DATA DIVISION.
WORKING-STORAGE SECTION.
01 x PICPICTURE IS 9(3) VALUE IS 3.
01 y PICPICTURE IS 9(3) VALUE IS 2.
PROCEDURE DIVISION.
DISPLAY myMultiply(x, y).
Line 896 ⟶ 1,318:
DATA DIVISION.
LINKAGE SECTION.
01 x PICPICTURE IS 9(3).
01 y PICPICTURE IS 9(3).
01 z picPICTURE IS 9(9).
PROCEDURE DIVISION USING x, y RETURNING z.
MULTIPLY x BY y GIVING z.
EXIT FUNCTIONGOBACK.
END FUNCTION myMultiply.</langsyntaxhighlight>
 
=={{header|Coco}}==
Line 908 ⟶ 1,330:
As CoffeeScript. In addition, Coco provides some syntactic sugar for accessing the <code>arguments</code> array reminiscent of Perl's <code>@_</code>:
 
<langsyntaxhighlight lang="coco">multiply = -> @@0 * @@1</langsyntaxhighlight>
 
Furthermore, when no parameter list is defined, the first argument is available as <code>it</code>:
 
<langsyntaxhighlight lang="coco">double = -> 2 * it</langsyntaxhighlight>
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="coffeescript">multiply = (a, b) -> a * b</langsyntaxhighlight>
 
=={{header|ColdFusion}}==
====Tag style====
<lang coldfusion><cffunction name="multiply" returntype="numeric">
<syntaxhighlight lang="coldfusion"><cffunction name="multiply" returntype="numeric">
<cfargument name="a" type="numeric">
<cfargument name="b" type="numeric">
<cfreturn a * b>
</cffunction></langsyntaxhighlight>
 
====Script style====
 
<syntaxhighlight lang="lisp">numeric function multiply(required numeric a, required numeric b){
return a * b;
}
</syntaxhighlight>
 
=={{header|Common Lisp}}==
Line 928 ⟶ 1,358:
===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.
<langsyntaxhighlight lang="lisp">(defun multiply (a b)
(* a b))
 
(multiply 2 3)</langsyntaxhighlight>
====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:
<langsyntaxhighlight lang="lisp">(define-compiler-macro multiply (&whole expr a b)
(if (and (constantp a) (constantp b))
(* (eval a) (eval b))
expr)) ;; no macro recursion if we just return expr; the job is done! </langsyntaxhighlight>
Lisp implementations do not have to honor compiler macros. Usually compilers make use of them, but evaluators do not.
 
Line 970 ⟶ 1,400:
 
Also, the DEFGENERIC is optional, since the first DEFMETHOD will define the generic function, but good practice.
<langsyntaxhighlight lang="lisp">
;;; terrific example coming
</syntaxhighlight>
</lang>
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">sub multiply(a: int32, b: int32): (rslt: int32) is
rslt := a * b;
end sub</langsyntaxhighlight>
 
=={{header|Creative Basic}}==
<lang Creative Basic>
DECLARE Multiply(N1:INT,N2:INT)
 
DEF A,B:INT
 
A=2:B=2
 
OPENCONSOLE
 
PRINT Multiply(A,B)
 
PRINT:PRINT"Press any key to close."
 
DO:UNTIL INKEY$<>""
 
CLOSECONSOLE
 
END
 
SUB Multiply(N1:INT,N2:INT)
 
DEF Product:INT
 
Product=N1*N2
 
RETURN Product
 
'Can also be written with no code in the subroutine and just RETURN N1*N2.
</lang>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">// A function:
int multiply1(int a, int b) {
return a * b;
Line 1,034 ⟶ 1,433:
import std.stdio;
writeln("2 * 3 = ", result);
}</langsyntaxhighlight>
Both the compile-time and run-time output:
<pre>6
Line 1,040 ⟶ 1,439:
 
=={{header|Dart}}==
<langsyntaxhighlight lang="d">main(){
print(multiply(1,2));
print(multiply2(1,2));
Line 1,057 ⟶ 1,456:
return num1 * num2;
}
</syntaxhighlight>
</lang>
 
=={{header|dc}}==
For dc, the functions (called macros) are limited to names from 'a' to 'z'
Create a function called 'm'
<syntaxhighlight lang ="dc">[*] sm</langsyntaxhighlight>
Use it (lm loads the function in 'm',x executes it, f shows the the stack.)
<langsyntaxhighlight lang="dc">3 4 lm x f
= 12</langsyntaxhighlight>
 
=={{header|Delphi}}==
In addition to what is shown in the section [[#Pascal|Pascal]], the following is possible too:
<langsyntaxhighlight lang="delphi">function multiply(a, b: integer): integer;
begin
result := a * b;
end;</langsyntaxhighlight>
 
=={{header|Diego}}==
<syntaxhighlight lang="diego">begin_funct({number}, multiply)_param({number}, a, b);
with_funct[]_calc([a]*[b]);
end_funct[];
 
me_msg()_funct(multiply)_param(1,2);</syntaxhighlight>
 
=={{header|DM}}==
Functions (called procs) may be derived from <code>proc</code>.
<syntaxhighlight lang="dm">proc/multiply(a, b)
return a * b
</syntaxhighlight>
 
=={{header|Draco}}==
Line 1,079 ⟶ 1,491:
return type of the function.
 
<langsyntaxhighlight lang="draco">proc multiply(word a, b) word:
a * b
corp</langsyntaxhighlight>
 
=={{header|Dragon}}==
<langsyntaxhighlight lang="dragon">func multiply(a, b) {
return a*b
}</langsyntaxhighlight>
 
=={{header|DWScript}}==
<langsyntaxhighlight Delphilang="delphi">function Multiply(a, b : Integer) : Integer;
begin
Result := a * b;
end;</langsyntaxhighlight>
 
=={{header|Dyalect}}==
<langsyntaxhighlight Dyalectlang="dyalect">func multiply(a, b) {
a * b
}</langsyntaxhighlight>
 
Using lambda syntax:
 
<langsyntaxhighlight Dyalectlang="dyalect">let multiply = (a, b) => a * b</langsyntaxhighlight>
 
=={{header|Déjà Vu}}==
<langsyntaxhighlight lang="dejavu">multiply a b:
* a b</langsyntaxhighlight>
 
=={{header|E}}==
<langsyntaxhighlight lang="e">def multiply(a, b) {
return a * b
}</langsyntaxhighlight>
(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:
<langsyntaxhighlight lang="e">def multiply := fn a, b { a * b }</langsyntaxhighlight>
This definition is identical to the previous except that the function object will not know its own name.
 
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
<lang>func multiply a b . r .
func r =multiply a *b b.
return a * b
.
callprint multiply 7 5 res
</syntaxhighlight>
print res</lang>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="lisp">
(define (multiply a b) (* a b)) → multiply ;; (1)
(multiply 1/3 666) → 222
Line 1,151 ⟶ 1,564:
 
multiply → (λ (_a _b) (#🔶_multiply)) ;; compiled function
</syntaxhighlight>
</lang>
 
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
module MultiplyExample {
static <Value extends Number> Value multiply(Value n1, Value n2) {
return n1 * n2;
}
 
void run() {
(Int i1, Int i2) = (7, 3);
Int i3 = multiply(i1, i2);
(Double d1, Double d2) = (2.7182818, 3.1415);
Double d3 = multiply(d1, d2);
@Inject Console console;
console.print($"{i1}*{i2}={i3}, {d1}*{d2}={d3}");
}
}
</syntaxhighlight>
 
{{out}}
<pre>
7*3=21, 2.7182818*3.1415=8.539482274700001
</pre>
 
=={{header|Efene}}==
<langsyntaxhighlight lang="efene">multiply = fn (A, B) {
A * B
}
Line 1,161 ⟶ 1,597:
run = fn () {
io.format("~p~n", [multiply(2, 5)])
}</langsyntaxhighlight>
 
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
multiply(a, b: INTEGER): INTEGER
do
Result := a*b
end
</syntaxhighlight>
</lang>
 
=={{header|Ela}}==
<langsyntaxhighlight Elalang="ela">multiply x y = x * y</langsyntaxhighlight>
Anonymous function:
<langsyntaxhighlight Elalang="ela">\x y -> x * y</langsyntaxhighlight>
 
=={{header|Elena}}==
<langsyntaxhighlight lang="elena">real multiply(real a, real b)
= a * b;</langsyntaxhighlight>
Anonymous function / closure:
<langsyntaxhighlight lang="elena">symbol f := (x,y => x * y);</langsyntaxhighlight>
Root closure:
<langsyntaxhighlight lang="elena">f(x,y){ ^ x * y }</langsyntaxhighlight>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule RosettaCode do
def multiply(x,y) do
x * y
Line 1,193 ⟶ 1,629:
end
 
RosettaCode.task</langsyntaxhighlight>
 
{{out}}
Line 1,201 ⟶ 1,637:
 
=={{header|Elm}}==
<syntaxhighlight lang="elm">
<lang Elm>
--There are multiple ways to create a function in Elm
 
Line 1,209 ⟶ 1,645:
--This is an anonymous function
\x y -> x*y
</syntaxhighlight>
</lang>
 
=={{header|Emacs Lisp}}==
<langsyntaxhighlight Lisplang="lisp">(defun multiply (x y)
(* x y))</langsyntaxhighlight>
 
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.
 
<langsyntaxhighlight Lisplang="lisp">(defun multiply (x y)
"Return the product of X and Y."
(* x y))</langsyntaxhighlight>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
fun multiply = var by var a, var b
return a * b
end
writeLine(multiply(6, 7))
writeLine(multiply("can", 2))
</syntaxhighlight>
{{out}}
<pre>
42
cancan
</pre>
 
=={{header|Erlang}}==
===Using case, multiple lines===
<langsyntaxhighlight lang="erlang">% Implemented by Arjun Sunel
-module(func_definition).
-export([main/0]).
Line 1,234 ⟶ 1,684:
case {A,B} of
{A, B} -> A * B
end.</langsyntaxhighlight>
{{out}}
<pre>12
Line 1,240 ⟶ 1,690:
</pre>
===In a single line===
<langsyntaxhighlight lang="erlang">
-module(func_definition).
-export([main/0]).
Line 1,248 ⟶ 1,698:
io :format("~p~n",[K]).
multiply(A,B) -> A * B.</langsyntaxhighlight>
The output is the same.
 
Line 1,282 ⟶ 1,732:
 
=={{header|Euphoria}}==
<langsyntaxhighlight Euphorialang="euphoria">function multiply( atom a, atom b )
return a * b
end function</langsyntaxhighlight>
If you declare the arguments as <code>object</code> then sequence comprehension kicks in:
<langsyntaxhighlight Euphorialang="euphoria">function multiply( object a, object b )
return a * b
end function
Line 1,297 ⟶ 1,747:
? multiply( a, b )
? multiply( a, 7 )
? multiply( 10.39564, b )</langsyntaxhighlight>
{{out}}
<pre>81
Line 1,307 ⟶ 1,757:
=={{header|F Sharp|F#}}==
The default will be an integer function but you can specify other types as shown:
<langsyntaxhighlight lang="fsharp">let multiply x y = x * y // integer
let fmultiply (x : float) (y : float) = x * y</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">: multiply ( a b -- a*b ) * ;</langsyntaxhighlight>
 
=={{header|Falcon}}==
<langsyntaxhighlight lang="falcon">function sayHiTo( name )
> "Hi ", name
end</langsyntaxhighlight>
 
=={{header|FALSE}}==
<langsyntaxhighlight lang="false">[*] {anonymous function to multiply the top two items on the stack}
m: {binding the function to one of the 26 available symbol names}
2 3m;! {executing the function, yielding 6}</langsyntaxhighlight>
 
=={{header|Fantom}}==
<langsyntaxhighlight lang="fantom">class FunctionDefinition
{
public static Void main ()
Line 1,331 ⟶ 1,781:
echo ("Multiply 2 and 4: ${multiply(2, 4)}")
}
}</langsyntaxhighlight>
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">Func Multiply(a, b) = a*b.</langsyntaxhighlight>
 
=={{header|Fexl}}==
<langsyntaxhighlight lang="fexl">\multiply=(\x\y * x y)</langsyntaxhighlight>
Or if I'm being cheeky:
<langsyntaxhighlight lang="fexl">\multiply=*</langsyntaxhighlight>
 
=={{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).
<syntaxhighlight lang ="fish">2[*]</langsyntaxhighlight>
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: fmultiply ( F: a b -- F: c ) F* ;
: multiply ( a b -- c ) * ;</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="fortran"> XMULTF(X,Y)=X*Y</langsyntaxhighlight>
And for interger multiplication:
<langsyntaxhighlight lang="fortran"> MULTF(I,J)=I*J</langsyntaxhighlight>
 
In FORTRAN IV, FORTRAN 66 or later, define a function:
<langsyntaxhighlight lang="fortran">FUNCTION MULTIPLY(X,Y)
REAL MULTIPLY, X, Y
MULTIPLY = X * Y
END</langsyntaxhighlight>
And for integer multiplication:
<langsyntaxhighlight lang="fortran">FUNCTION MULTINT(X,Y)
INTEGER MULTINT, X, Y
MULTINT = X * Y
END</langsyntaxhighlight>
 
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:
<langsyntaxhighlight lang="fortran">module elemFunc
contains
elemental function multiply(x, y)
Line 1,374 ⟶ 1,824:
multiply = x * y
end function multiply
end module elemFunc</langsyntaxhighlight>
<langsyntaxhighlight lang="fortran">program funcDemo
use elemFunc
Line 1,384 ⟶ 1,834:
z = multiply(x,y) ! element-wise invocation only works with elemental function
end program funcDemo</langsyntaxhighlight>
It is worth noting that Fortran can call functions (and subroutines) using named arguments; e.g. we can call multiply in the following way:
<langsyntaxhighlight 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)</langsyntaxhighlight>
(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:
<langsyntaxhighlight lang="fortran">module elemFunc
contains
elemental function multiply(x, y) result(z)
Line 1,398 ⟶ 1,848:
z = x * y
end function multiply
end module elemFunc</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
 
Function multiply(d1 As Double, d2 As Double) As Double
Return d1 * d2
End Function</lang>
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).
 
Alternatively, one could write a macro though this wouldn't be type-safe:
 
<lang freebasic>#Define multiply(d1, d2) (d1) * (d2)</lang>
 
=={{header|Free Pascal}}==
Line 1,420 ⟶ 1,857:
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>:
<langsyntaxhighlight lang="delphi">function multiply(a, b: integer): integer;
begin
exit(a * b);
end;</langsyntaxhighlight>
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>.
Line 1,429 ⟶ 1,866:
 
=={{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.
<lang frink>multiply[x,y] := x*y</lang>
<syntaxhighlight lang="frink">multiply[x,y] := x*y</syntaxhighlight>
 
=={{header|Futhark}}==
 
<syntaxhighlight lang="futhark">
<lang Futhark>
let multiply (x: i32, y: i32) : i32 = x * y
</syntaxhighlight>
</lang>
 
=={{header|FutureBasic}}==
<lang futurebasic>window 1
 
local fn multiply( a as long, b as long ) as long
end fn = a * b
 
print fn multiply( 3, 9 )
 
HandleEvents</lang>
Output:
<pre>
27
</pre>
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=bc93236474d9937217dd4117026f7441 Click this link to run this code]'''
<lang gambas>Public Sub Main()
 
Print Multiply(56, 4.66)
 
End
 
Public Sub Multiply(f1 As Float, f2 As Float) As Float
 
Return f1 * f2
 
End</lang>
Output:
<pre>
260.96
</pre>
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap">multiply := function(a, b)
return a*b;
end;</langsyntaxhighlight>
 
=={{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:
<langsyntaxhighlight GMLlang="gml">#define multiply
a = argument0
b = argument1
return(a * b)</langsyntaxhighlight>
 
=={{header|Gnuplot}}==
<langsyntaxhighlight Gnuplotlang="gnuplot">multiply(x,y) = x*y
 
# then for example
print multiply(123,456)</langsyntaxhighlight>
 
=={{header|Go}}==
Line 1,491 ⟶ 1,897:
 
The return statement can contain an expression of the function return type:
<langsyntaxhighlight lang="go">func multiply(a, b float64) float64 {
return a * b
}</langsyntaxhighlight>
Alternatively, if the return value is named, the return statement does not require an expression:
<langsyntaxhighlight lang="go">func multiply(a, b float64) (z float64) {
z = a * b
return
}</langsyntaxhighlight>
 
=={{header|Golfscript}}==
<syntaxhighlight lang ="golfscript">{*}:multiply;</langsyntaxhighlight>
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">def multiply = { x, y -> x * y }</langsyntaxhighlight>
Test Program:
<langsyntaxhighlight lang="groovy">println "x * y = 20 * 50 = ${multiply 20, 50}"</langsyntaxhighlight>
{{out}}
<pre>x * y = 20 * 50 = 1000</pre>
 
=={{header|Halon}}==
<langsyntaxhighlight lang="halon">function multiply( $a, $b )
{
return $a * $b;
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">multiply x y = x * y</langsyntaxhighlight>
Alternatively, with help of auto-currying,
<langsyntaxhighlight lang="haskell">multiply = (*)</langsyntaxhighlight>
You can use [[lambda-function]]
<langsyntaxhighlight lang="haskell">multiply = \ x y -> x*y</langsyntaxhighlight>
 
=={{header|Haxe}}==
<langsyntaxhighlight lang="haxe">function multiply(x:Float, y:Float):Float{
return x * y;
}</langsyntaxhighlight>
 
=={{header|hexiscript}}==
<langsyntaxhighlight lang="hexiscript">fun multiply a b
return a * b
endfun</langsyntaxhighlight>
 
=={{header|HicEst}}==
<langsyntaxhighlight lang="hicest">FUNCTION multiply(a, b)
multiply = a * b
END</langsyntaxhighlight>
 
=={{header|HolyC}}==
<langsyntaxhighlight lang="holyc">F64 Multiply(F64 a, F64 b) {
return a * b;
}
Line 1,545 ⟶ 1,951:
F64 x;
x = Multiply(42, 13.37);
Print("%5.2f\n", x);</langsyntaxhighlight>
 
=={{header|Hy}}==
Function definition:
<langsyntaxhighlight lang="clojure">(defn multiply [a b]
(* a b))</langsyntaxhighlight>
Lambda definition:
<langsyntaxhighlight lang="clojure">(def multiply (fn [a b] (* a b)))</langsyntaxhighlight>
 
=={{header|i}}==
<syntaxhighlight lang="i">
<lang i>
concept multiply(a, b) {
return a*b
}
</syntaxhighlight>
</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure multiply(a,b)
return a * b
end</langsyntaxhighlight>
 
=={{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:
<langsyntaxhighlight lang="idl">function multiply ,a,b
return, a* b
end</langsyntaxhighlight>
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:
<langsyntaxhighlight lang="idl">function multiply ,a,b
return, product([a, b])
end</langsyntaxhighlight>
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:
<langsyntaxhighlight lang="idl">function multiply ,a,b
return, a # b
end</langsyntaxhighlight>
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}}==
<langsyntaxhighlight lang="inform6">[ multiply a b;
return a * b;
];</langsyntaxhighlight>
 
=={{header|Inform 7}}==
<langsyntaxhighlight lang="inform7">To decide which number is (A - number) multiplied by (B - number):
decide on A * B.</langsyntaxhighlight>
 
=={{header|Io}}==
<langsyntaxhighlight lang="io">multiply := method(a,b,a*b)</langsyntaxhighlight>
 
=={{header|IWBASIC}}==
<lang IWBASIC>
'1. Not Object Oriented Program
 
DECLARE Multiply(N1:INT,N2:INT),INT
DEF A,B:INT
A=2:B=2
OPENCONSOLE
PRINT Multiply(A,B)
 
PRINT
'When compiled as a console only program, a press any key to continue is automatic.
CLOSECONSOLE
END
SUB Multiply(N1:INT,N2:INT),INT
DEF Product:INT
Product=N1*N2
RETURN Product
ENDSUB
 
'Can also be written with no code in the subroutine and just RETURN N1*N2.
 
----
 
'2. Not Object Oriented Program Using A Macro
 
$MACRO Multiply (N1,N2) (N1*N2)
 
DEF A,B:INT
 
A=5:B=5
 
OPENCONSOLE
 
PRINT Multiply (A,B)
 
PRINT
 
'When compiled as a console only program, a press any key to continue is automatic.
CLOSECONSOLE
 
END
 
----
 
'3. In An Object Oriented Program
 
CLASS Associate
'functions/methods
DECLARE Associate:'object constructor
DECLARE _Associate:'object destructor
'***Multiply declared***
DECLARE Multiply(UnitsSold:UINT),UINT
'members
DEF m_Price:UINT
DEF m_UnitsSold:UINT
DEF m_SalesTotal:UINT
ENDCLASS
 
DEF Emp:Associate
 
m_UnitsSold=10
 
Ass.Multiply(m_UnitsSold)
 
OPENCONSOLE
 
PRINT"Sales total: ",:PRINT"$"+LTRIM$(STR$(Emp.m_SalesTotal))
 
PRINT
 
CLOSECONSOLE
 
END
 
'm_price is set in constructor
SUB Associate::Multiply(UnitsSold:UINT),UINT
m_SalesTotal=m_Price*UnitsSold
RETURN m_SalesTotal
ENDSUB
 
SUB Associate::Associate()
m_Price=10
ENDSUB
 
SUB Associate::_Associate()
'Nothing to cleanup
ENDSUB
 
</lang>
 
=={{header|J}}==
<langsyntaxhighlight lang="j">multiply=: *</langsyntaxhighlight>
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):
<langsyntaxhighlight Jlang="j">multiply=: dyad define
x * y
)</langsyntaxhighlight>
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,712 ⟶ 2,017:
=={{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.
<langsyntaxhighlight lang="java">public class Math
{
public static int multiply( int a, int b) { return a*b; }
public static double multiply(double a, double b) { return a*b; }
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
===ES1-*===
Function Declaration
<langsyntaxhighlight lang="javascript">function multiply(a, b) {
return a*b;
}</langsyntaxhighlight>
 
===ES3-*===
Function Expression
<langsyntaxhighlight lang="javascript">var multiply = function(a, b) {
return a * b;
};</langsyntaxhighlight>
 
Named Function Expression
<langsyntaxhighlight lang="javascript">var multiply = function multiply(a, b) {
return a * b;
};</langsyntaxhighlight>
 
Method Definition
<langsyntaxhighlight lang="javascript">var o = {
multiply: function(a, b) {
return a * b;
}
};</langsyntaxhighlight>
 
===ES5-*===
Accessors
<langsyntaxhighlight lang="javascript">var o = {
get foo() {
return 1;
Line 1,752 ⟶ 2,057:
// do things with value
}
};</langsyntaxhighlight>
 
 
===ES6-*===
Arrow Function
<langsyntaxhighlight lang="javascript">var multiply = (a, b) => a * b;
var multiply = (a, b) => { return a * b };
</syntaxhighlight>
</lang>
 
Concise Body Method Definition
<langsyntaxhighlight lang="javascript">var o = {
multiply(a, b) {
return a * b;
}
};</langsyntaxhighlight>
 
Generator Functions
<langsyntaxhighlight lang="javascript">function * generator() {
yield 1;
}</langsyntaxhighlight>
 
=={{header|Joy}}==
<langsyntaxhighlight lang="joy">DEFINE multiply == * .</langsyntaxhighlight>
 
=={{header|jq}}==
Example of a simple function definition:<langsyntaxhighlight lang="jq">def multiply(a; b): a*b;</langsyntaxhighlight>
Example of the definition of an inner function:<langsyntaxhighlight lang="jq"># 2 | generate(. * .) will generate 2, 4, 16, 256, ...
def generate(f): def r: ., (f | r); r;</langsyntaxhighlight>
The previous example (generate/1) also illustrates that a function argument can be a function or composition of functions. Here is another example:<langsyntaxhighlight lang="jq">def summation(f): reduce .[] as $x (0; . + ($x|f));</langsyntaxhighlight>
<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:<langsyntaxhighlight lang="jq">summation( .h * .w)</langsyntaxhighlight>
 
=={{header|Julia}}==
Line 1,787 ⟶ 2,092:
General function definition:
 
<langsyntaxhighlight lang="julia">function multiply(a::Number, b::Number)
return a * b
end</langsyntaxhighlight>
 
Julia also supports `assignment` definition as shorthand:
 
<langsyntaxhighlight lang="julia">multiply(a, b) = a * b</langsyntaxhighlight>
 
And lambda calculus:
 
<langsyntaxhighlight lang="julia">multiply = (a, b) -> a * b</langsyntaxhighlight>
 
=={{header|Kaya}}==
<langsyntaxhighlight lang="kaya">program test;
 
// A function definition in Kaya:
Line 1,810 ⟶ 2,115:
Void main() {
putStrLn(string( multiply(2, 3) ));
}</langsyntaxhighlight>
 
=={{header|Klingphix}}==
<langsyntaxhighlight Klingphixlang="klingphix">:multiply * ;
 
2 3 multiply print { 6 }</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="kotlin">// One-liner
fun multiply(a: Int, b: Int) = a * b
 
Line 1,824 ⟶ 2,129:
fun multiplyProper(a: Int, b: Int): Int {
return a * b
}</langsyntaxhighlight>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
<lang Scheme>
{def multiply
{lambda {:a :b}
Line 1,844 ⟶ 2,149:
-> 720
 
</syntaxhighlight>
</lang>
 
=={{header|langurLang}}==
=== Function decleration ===
A function body may use curly braces, but it is not required if it is a single expression.
<syntaxhighlight lang="lang">
fp.multiply = ($a, $b) -> {
return parser.op($a * $b)
}
</syntaxhighlight>
 
=== One-line function decleration ===
A return statement may be used, but a function's last value is its implicit return value.
<syntaxhighlight lang="lang">
fp.multiply = ($a, $b) -> return parser.op($a * $b)
</syntaxhighlight>
 
=== Function decleration by using operator functions ===
Functions defined with explicit parameters may be closures, and those defined with implied parameters are not.
<syntaxhighlight lang="lang">
fp.multiply = fn.mul
</syntaxhighlight>
 
=== Function decleration by using combinator functions ===
Langur functions are first-order. They are pure in terms of setting values, though not in terms of I/O.
Combinator functions can be called partially, fn.argCnt2 is used to force the caller to provide 2 arguments to prevent partially calling fp.multiply
<syntaxhighlight lang="lang">
fp.multiply = fn.argCnt2(fn.combA2(fn.mul))
</syntaxhighlight>
 
=== Function decleration with call by pointer ===
=== explicit parameters ===
<syntaxhighlight lang="lang">
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 fp.multiply = f(.x$[a], .y$[b]) .x-> x .y{
return parser.op($*a * $*b) # Pointers can be dereferenced by using *
.multiply(3, 4)</lang>
}
</syntaxhighlight>
 
=={{header|langur}}==
Langur functions are first-order. They are pure in terms of setting values and in terms of I/O (unless declared impure).
 
A return statement may be used, but a function's last value is its implicit return value.
 
=== implied parameters ===
Parameters are implieddefined whenwithin parentheses after the ffn token is not immediately followed by parentheses without spacing. TheTo impliedspecify order of impliedno parameters, isuse basedan onempty the string sort orderset of their names, not their order within the functionparentheses.
<langsyntaxhighlight lang="langur">val .multiply = f fn(.x, .y) { .x * .y }
.multiply(3, 4)</langsyntaxhighlight>
 
=== operator implied functions ===
Operator implied functions are built using an infix operator between curly braces on an ffn token.
 
<syntaxhighlight lang="langur">val .multiply = fn{*}
{{works with|langur|0.6.6}}
.multiply(3, 4)</syntaxhighlight>
<lang langur>val .multiply = f{x}
.multiply(3, 4)</lang>
 
=== nil left partially implied functions ===
These are built with an infix operator and onea right-hand operand inside the ffn{...} tokens.
 
<syntaxhighlight lang="langur">val .times3 = fn{* 3}
{{works with|langur|0.8.11}}
<lang langur>valmap .times3, =[1, f{x2, 3}]</syntaxhighlight>
 
map .times3, [1, 2, 3]</lang>
=== impure functions (I/O) ===
Impure functions must be declared as such.
<syntaxhighlight>val .writeit = impure fn(.x) { writeln .x }</syntaxhighlight>
 
Impure functions cannot be passed to pure functions.
 
=={{header|Lasso}}==
Line 1,883 ⟶ 2,214:
Lasso supports multiple dispatch — signature definitions determine which method will be invoked.
 
<langsyntaxhighlight Lassolang="lasso">define multiply(a,b) => {
return #a * #b
}</langsyntaxhighlight>
 
As this function is so simple it can also be represented like so:
 
<langsyntaxhighlight Lassolang="lasso">define multiply(a,b) => #a * #b</langsyntaxhighlight>
 
Using multiple dispatch, different functions will be invoked depending on the functions input.
 
<langsyntaxhighlight Lassolang="lasso">// Signatures that convert second input to match first input
define multiply(a::integer,b::any) => #a * integer(#b)
define multiply(a::decimal,b::any) => #a * decimal(#b)
 
// Catch all signature
define multiply(a::any,b::any) => decimal(#a) * decimal(#b)</langsyntaxhighlight>
 
=={{header|Latitude}}==
Line 1,904 ⟶ 2,235:
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.
 
<langsyntaxhighlight lang="latitude">multiply := { $1 * $2. }.</langsyntaxhighlight>
 
Calling a method is done either with parentheses or with a colon.
 
<langsyntaxhighlight lang="latitude">multiply (2, 3).
multiply: 2, 3.</langsyntaxhighlight>
 
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>.
 
<langsyntaxhighlight lang="latitude">multiply := proc { $1 * $2. }.
multiply call (2, 3).
multiply call: 2, 3.</langsyntaxhighlight>
 
=={{header|LFELDPL}}==
<syntaxhighlight lang="ldpl">data:
<lang lisp>
n is number
(defun mutiply (a b)
(* a b))
</lang>
 
procedure:
=={{header|Liberty BASIC}}==
sub multiply
<lang lb>' define & call a function
parameters:
x is number
y is number
result is number
procedure:
in result solve x * y
end sub
 
# call the bare sub-procedure
print multiply( 3, 1.23456)
call multiply with 3 4 n
display n lf
 
# create a statement for it
wait
create statement "multiply $ by $ in $" executing multiply
 
function multiply( m1,3 by 4 in m2)n
display n lf
multiply =m1 *m2
</syntaxhighlight>
end function
{{out}}
<pre>
12
12
</pre>
 
=={{header|LFE}}==
end</lang>
<syntaxhighlight lang="lisp">
(defun mutiply (a b)
(* a b))
</syntaxhighlight>
 
=={{header|Lily}}==
<langsyntaxhighlight Lilylang="lily">define multiply(a: Integer, b: Integer): Integer
{
return a * b
}</langsyntaxhighlight>
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">on multiply (a, b)
return a * b
end</langsyntaxhighlight>
 
=={{header|LiveCode}}==
LiveCode has a built-in method called multiply, so there is an extra y to avoid an error.
<langsyntaxhighlight LiveCodelang="livecode">function multiplyy n1 n2
return n1 * n2
end multiplyy
 
put multiplyy(2,5) -- = 10</langsyntaxhighlight>
 
=={{header|Locomotive Basic}}==
<lang locobasic>10 DEF FNmultiply(x,y)=x*y
20 PRINT FNmultiply(2,PI)</lang>
Function names are always preceded by "FN" in Locomotive BASIC. Also, PI is predefined by the interpreter as 3.14159265.
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">to multiply :x :y
output :x * :y
end</langsyntaxhighlight>
 
=={{header|LSE64}}==
<langsyntaxhighlight lang="lse64">multiply : *
multiply. : *. # floating point</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">function multiply( a, b )
return a * b
end</langsyntaxhighlight>
 
=={{header|Lucid}}==
<langsyntaxhighlight lang="lucid">multiply(x,y) = x * y</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
===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.
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
Module Multiply (a, b) {
Line 2,012 ⟶ 2,355:
Call Checkit, 20, 50
Print Number=1000
</syntaxhighlight>
</lang>
 
===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.
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
\\ functions can shange by using a newer definition
Line 2,083 ⟶ 2,426:
}
Checkit
</syntaxhighlight>
</lang>
 
===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)
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
A$=Lambda$ N$="Hello There" (x) ->{
Line 2,110 ⟶ 2,453:
B$=List$("Hello", "Rosetta", "Function")
Print B$(1)="Hello"
</syntaxhighlight>
</lang>
 
=={{header|M4}}==
<langsyntaxhighlight M4lang="m4">define(`multiply',`eval($1*$2)')
 
multiply(2,3)</langsyntaxhighlight>
 
=={{header|MAD}}==
MAD supports two types of function declarations. One simply evaluates an expression:
<langsyntaxhighlight lang="mad"> INTERNAL FUNCTION MULT.(A,B) = A * B</langsyntaxhighlight>
 
Another allows multiple lines to be executed:
<langsyntaxhighlight lang="mad"> INTERNAL FUNCTION(A, B)
ENTRY TO MULT.
FUNCTION RETURN A * B
END OF FUNCTION</langsyntaxhighlight>
 
There are several quirks here. First, the length of any identifier must not be longer than six
Line 2,141 ⟶ 2,484:
=={{header|Make}}==
In makefile, a function may be defined as a rule, with recursive make used to retrieve the returned value.
<langsyntaxhighlight lang="make">A=1
B=1
 
multiply:
@expr $(A) \* $(B)</langsyntaxhighlight>
Invoking it
<langsyntaxhighlight lang="make">make -f mul.mk multiply A=100 B=3
> 300</langsyntaxhighlight>
Using gmake, the define syntax is used to define a new function
{{works with|gmake}}
<langsyntaxhighlight lang="make">A=1
B=1
 
Line 2,161 ⟶ 2,504:
@$(call multiply, $(A), $(B))
 
|gmake -f mul.mk do A=5 B=3</langsyntaxhighlight>
 
=={{header|Maple}}==
<langsyntaxhighlight lang="maple">multiply:= (a, b) -> a * b;</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Line 2,170 ⟶ 2,513:
 
Defining a function as a transformation rule:
<langsyntaxhighlight Mathematicalang="mathematica">multiply[a_,b_]:=a*b</langsyntaxhighlight>
Defining a pure function:
<langsyntaxhighlight Mathematicalang="mathematica">multiply=#1*#2&</langsyntaxhighlight>
 
=={{header|Maxima}}==
<langsyntaxhighlight Maximalang="maxima">f(a, b):= a*b;</langsyntaxhighlight>
 
=={{header|MAXScript}}==
<langsyntaxhighlight lang="maxscript">fn multiply a b =
(
a * b
)</langsyntaxhighlight>
 
=={{header|Mercury}}==
<langsyntaxhighlight Mercurylang="mercury">% Module ceremony elided...
:- func multiply(integer, integer) = integer.
multiply(A, B) = A * B.</langsyntaxhighlight>
 
=={{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>.
<langsyntaxhighlight lang="metafont">primarydef a mult b = a * b enddef;</langsyntaxhighlight>
<langsyntaxhighlight lang="metafont">t := 3 mult 5; show t; end</langsyntaxhighlight>
The '''primarydef''' allows to build binary operators with the same priority as *. For a more generic macro, we can use instead
<langsyntaxhighlight lang="metafont">def mult(expr a, b) = (a * b) enddef;
t := mult(2,3);
show t;
end</langsyntaxhighlight>
 
=={{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>.
<syntaxhighlight lang ="min">'* :multiply</langsyntaxhighlight>
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">multiply = function(x,y)
return x*y
end function
 
print multiply(6, 7)</langsyntaxhighlight>
{{out}}
<pre>42</pre>
Line 2,225 ⟶ 2,568:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">PROCEDURE Multiply(a, b: INTEGER): INTEGER;
BEGIN
RETURN a * b
END Multiply;</langsyntaxhighlight>
 
=={{header|Modula-3}}==
<langsyntaxhighlight lang="modula3">PROCEDURE Multiply(a, b: INTEGER): INTEGER =
BEGIN
RETURN a * b;
END Multiply;</langsyntaxhighlight>
 
=={{header|MUMPS}}==
<langsyntaxhighlight MUMPSlang="mumps">MULTIPLY(A,B);Returns the product of A and B
QUIT A*B</langsyntaxhighlight>
 
=={{header|Nanoquery}}==
<langsyntaxhighlight lang="nanoquery">def multiply(a, b)
return a * b
end</langsyntaxhighlight>
 
=={{header|Neko}}==
<langsyntaxhighlight Nekolang="neko">var multiply = function(a, b) {
a * b
}
 
$print(multiply(2, 3))</langsyntaxhighlight>
 
'''Output:'''
Line 2,256 ⟶ 2,599:
 
=={{header|Nemerle}}==
<langsyntaxhighlight Nemerlelang="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
return multiply(a, b)
}</langsyntaxhighlight>
 
=={{header|NESL}}==
<langsyntaxhighlight lang="nesl">function multiply(x, y) = x * y;</langsyntaxhighlight>
The NESL system responds by reporting the type it has inferred for the function:
<pre>multiply = fn : (a, a) -> a :: (a in number)</pre>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols binary
 
Line 2,290 ⟶ 2,633:
 
product = multiplicand * multiplier
return product</langsyntaxhighlight>
{{out}}
<pre>
Line 2,299 ⟶ 2,642:
 
=={{header|NewLISP}}==
<langsyntaxhighlight NewLISPlang="newlisp">> (define (my-multiply a b) (* a b))
(lambda (a b) (* a b))
> (my-multiply 2 3)
6</langsyntaxhighlight>
 
=={{header|Nial}}==
Using variables
<langsyntaxhighlight lang="nial">multiply is operation a b {a * b}</langsyntaxhighlight>
Using it
<langsyntaxhighlight lang="nial">|multiply 2 3
=6</langsyntaxhighlight>
Point free form
<syntaxhighlight lang ="nial">mul is *</langsyntaxhighlight>
Using it
<langsyntaxhighlight lang="nial">|mul 3 4
=12</langsyntaxhighlight>
Nial also allows creation of operators
<langsyntaxhighlight lang="nial">multiply is op a b {a * b}</langsyntaxhighlight>
Using it.
<langsyntaxhighlight lang="nial">|2 multiply 3
=6
|multiply 2 3
=6</langsyntaxhighlight>
Since this is an array programming language, any parameters can be arrays too
<langsyntaxhighlight lang="nial">|mul 3 [1,2]
=3 6
|mul [1,2] [10,20]
=10 40</langsyntaxhighlight>
 
=={{header|Nim}}==
Nim has a magic variable, `result`, which can be used as a substitute for `return`. The `result` variable will be returned implicitly.
<langsyntaxhighlight lang="nim">proc multiply(a, b: int): int =
result = a * b</langsyntaxhighlight>
Here is the same function but with the use of the `return` keyword.
<langsyntaxhighlight lang="nim">proc multiply(a, b: int): int =
return a * b</langsyntaxhighlight>
The last statement in a function implicitly is the result value:
<langsyntaxhighlight lang="nim">proc multiply(a, b: int): int = a * b</langsyntaxhighlight>
 
=={{header|OASYS}}==
<langsyntaxhighlight lang="oasys_oac">method int multiply int x int y {
return x * y
}</langsyntaxhighlight>
 
=={{header|OASYS Assembler}}==
OASYS Assembler requires a prefix and suffix on names to indicate their types (an omitted suffix means a void type).
<langsyntaxhighlight lang="oasys_oaa">[&MULTIPLY#,A#,B#],A#<,B#<MUL RF</langsyntaxhighlight>
 
=={{header|Oberon-2}}==
Oberon-2 uses procedures, and has a special procedure called a "Function Procedure" used to return a value.
<langsyntaxhighlight lang="oberon2">PROCEDURE Multiply(a, b: INTEGER): INTEGER;
BEGIN
RETURN a * b;
END Multiply;</langsyntaxhighlight>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">function : Multiply(a : Float, b : Float) ~, Float {
return a * b;
}</langsyntaxhighlight>
 
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">let int_multiply x y = x * y
let float_multiply x y = x *. y</langsyntaxhighlight>
 
=={{header|Octave}}==
<langsyntaxhighlight lang="octave">function r = mult(a, b)
r = a .* b;
endfunction</langsyntaxhighlight>
 
=={{header|Oforth}}==
Line 2,375 ⟶ 2,718:
If necessary, we can create a function with name multiply, but, it will just call *
 
<syntaxhighlight lang Oforth="oforth">: multiply * ;</langsyntaxhighlight>
 
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 :
 
<langsyntaxhighlight Oforthlang="oforth">: multiply2(a, b) a b * ;</langsyntaxhighlight>
 
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,385 ⟶ 2,728:
=={{header|Ol}}==
Function creation implemented using keyword 'lambda'. This created anonymous function can be saved into local or global variable for further use.
<langsyntaxhighlight lang="scheme">
(lambda (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.
<langsyntaxhighlight lang="scheme">
(define multiply (lambda (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.
<langsyntaxhighlight lang="scheme">
(let multiply ((x n) (y m))
(* x y))
Line 2,411 ⟶ 2,754:
(print (multiply 7 8))
; ==> 56
</syntaxhighlight>
</lang>
 
=={{header|OOC}}==
<langsyntaxhighlight lang="ooc">
multiply: func (a: Double, b: Double) -> Double {
a * b
}
</syntaxhighlight>
</lang>
 
=={{header|ooRexx}}==
===Internal Procedure===
<langsyntaxhighlight lang="rexx">SAY multiply(5, 6)
EXIT
multiply:
PROCEDURE
PARSE ARG x, y
RETURN x*y</langsyntaxhighlight>
===::Routine Directive===
<langsyntaxhighlight lang="oorexx">
say multiply(5, 6)
::routine multiply
use arg x, y
return x *y </langsyntaxhighlight>
===Accomodate large factors===
<langsyntaxhighlight lang="oorexx">say multiply(123456789,987654321)
say multiply_long(123456789,987654321)
::routine multiply
Line 2,443 ⟶ 2,786:
use arg x, y
Numeric Digits (length(x)+length(y))
return x *y </langsyntaxhighlight>
{{out}}
<pre>1.21932631E+17
Line 2,449 ⟶ 2,792:
 
=={{header|OpenEdge/Progress}}==
<langsyntaxhighlight Progresslang="progress (Openedgeopenedge ABLabl)">function multiply returns dec (a as dec , b as dec ):
return a * b .
end.</langsyntaxhighlight>
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">fun {Multiply X Y}
X * Y
end</langsyntaxhighlight>
Or by exploiting first-class functions:
<langsyntaxhighlight lang="oz">Multiply = Number.'*'</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">multiply(a,b)=a*b;</langsyntaxhighlight>
or
<langsyntaxhighlight lang="parigp">multiply=(a,b)->a*b;</langsyntaxhighlight>
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:
<langsyntaxhighlight lang="parigp">multiply={(a,b)->a*b;}</langsyntaxhighlight>
which will return a function which calculates but does not return the product.
 
Line 2,471 ⟶ 2,814:
''see also: [[#Delphi|Delphi]] and [[#Free Pascal|Free Pascal]]''
 
<langsyntaxhighlight lang="pascal">function multiply(a, b: real): real;
begin
multiply := a * b
end;</langsyntaxhighlight>
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.
Line 2,480 ⟶ 2,823:
=={{header|Perl}}==
The most basic form:
<langsyntaxhighlight lang="perl">sub multiply { return $_[0] * $_[1] }</langsyntaxhighlight>
or simply:
<langsyntaxhighlight lang="perl">sub multiply { $_[0] * $_[1] }</langsyntaxhighlight>
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]:
<langsyntaxhighlight lang="perl">sub multiply( $$ )
{
my ($a, $b) = @_;
return $a * $b;
}</langsyntaxhighlight>
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):
<langsyntaxhighlight lang="perl">use experimental 'signatures';
sub multiply ($x, $y) {
return $x * $y;
}</langsyntaxhighlight>
 
=={{header|Phix}}==
{{libheader|Phix/basics}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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;">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>
<!--</langsyntaxhighlight>-->
 
=={{header|Phixmonti}}==
<syntaxhighlight lang Phixmonti="phixmonti">def multiply * enddef</langsyntaxhighlight>
 
=={{header|PHL}}==
 
<langsyntaxhighlight lang="phl">@Integer multiply(@Integer a, @Integer b) [
return a * b;
]</langsyntaxhighlight>
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">function multiply( $a, $b )
{
return $a * $b;
}</langsyntaxhighlight>
 
=={{header|Picat}}==
<langsyntaxhighlight lang="php">multiply(A, B) = A*B.
</syntaxhighlight>
</lang>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de multiply (A B)
(* A B) )</langsyntaxhighlight>
 
=={{header|Pike}}==
<langsyntaxhighlight lang="pike">int multiply(int a, int b){
return a * b;
}</langsyntaxhighlight>
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">PRODUCT: procedure (a, b) returns (float);
declare (a, b) float;
return (a*b);
end PRODUCT;</langsyntaxhighlight>
 
=={{header|PL/SQL}}==
<langsyntaxhighlight lang="plsql">FUNCTION multiply(p_arg1 NUMBER, p_arg2 NUMBER) RETURN NUMBER
IS
v_product NUMBER;
Line 2,549 ⟶ 2,892:
v_product := p_arg1 * p_arg2;
RETURN v_product;
END;</langsyntaxhighlight>
 
=={{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>.
<langsyntaxhighlight lang="plainenglish">To multiply a number with another number:
Multiply the number by the other number.</langsyntaxhighlight>
 
=={{header|Pop11}}==
<langsyntaxhighlight lang="pop11">define multiply(a, b);
a * b
enddefine;</langsyntaxhighlight>
 
=={{header|PostScript}}==
Inbuilt:
<syntaxhighlight lang ="postscript">3 4 mul</langsyntaxhighlight>
Function would be:
<langsyntaxhighlight lang="postscript">/multiply{
/x exch def
/y exch def
x y mul =
}def</langsyntaxhighlight>
 
=={{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:
<langsyntaxhighlight lang="powershell">function multiply {
return $args[0] * $args[1]
}</langsyntaxhighlight>
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":
<langsyntaxhighlight lang="powershell">function multiply {
$args[0] * $args[1]
}</langsyntaxhighlight>
Furthermore, the function arguments can be stated and named explicitly:
<langsyntaxhighlight lang="powershell">function multiply ($a, $b) {
return $a * $b
}</langsyntaxhighlight>
There is also an alternative style for declaring parameters. The choice is mostly a matter of personal preference:
<langsyntaxhighlight lang="powershell">function multiply {
param ($a, $b)
return $a * $b
}</langsyntaxhighlight>
And the arguments can have an explicit type:
<langsyntaxhighlight lang="powershell">function multiply ([int] $a, [int] $b) {
return $a * $b
}</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="java">float multiply(float x, float y)
{
return x * y;
}</langsyntaxhighlight>
 
==={{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.
<langsyntaxhighlight lang="python">def multiply(x, y):
return x * y</langsyntaxhighlight>
 
=={{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):
<langsyntaxhighlight Prologlang="prolog">multiply(A, B, P) :- P is A * B.</langsyntaxhighlight>
This is what it looks like in use:
<langsyntaxhighlight Prologlang="prolog">go :-
multiply(5, 2, P),
format("The product is ~d.~n", [P]).</langsyntaxhighlight>
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:
<langsyntaxhighlight Prologlang="prolog">test_multiply :-
multiply(5, 2, 10), % this will pass
multiply(3, 4, 11). % this will not pass</langsyntaxhighlight>
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.
<langsyntaxhighlight Prologlang="prolog">:- use_module(library(function_expansion)).
 
user:function_expansion(multiply(A, B), P, P is A * B). % "function" definition
 
go :-
format("The product is ~d.~n", [multiply(5, 2)]).</langsyntaxhighlight>
 
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:
<langsyntaxhighlight Prologlang="prolog">go :-
A is 5*2,
format('The product is ~d.~n', [A]).</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<lang PureBasic>Procedure multiply(a,b)
ProcedureReturn a*b
EndProcedure</lang>
 
=={{header|Python}}==
Function definition:
<langsyntaxhighlight lang="python">def multiply(a, b):
return a * b</langsyntaxhighlight>
Lambda function definition:
<langsyntaxhighlight lang="python">multiply = lambda a, b: a * b</langsyntaxhighlight>
A callable class definition allows functions and classes to use the same interface:
<langsyntaxhighlight lang="python">class Multiply:
def __init__(self):
pass
Line 2,652 ⟶ 2,990:
 
multiply = Multiply()
print multiply(2, 4) # prints 8</langsyntaxhighlight>
(No extra functionality is shown in ''this'' class definition).
 
=={{header|Q}}==
<langsyntaxhighlight lang="q">multiply:{[a;b] a*b}</langsyntaxhighlight>
or
<syntaxhighlight lang ="q">multiply:{x*y}</langsyntaxhighlight>
or
<syntaxhighlight lang ="q">multiply:*</langsyntaxhighlight>
Using it
<langsyntaxhighlight lang="q">multiply[2;3]
6</langsyntaxhighlight>
 
=={{header|Quack}}==
You have several ways to define a function in Quack. You can do it by the classic way:
<langsyntaxhighlight lang="quack">fn multiply[ a; b ]
^ a * b
end</langsyntaxhighlight>
 
Using lambda-expressions:
<langsyntaxhighlight lang="quack">let multiply :- fn { a; b | a * b }</langsyntaxhighlight>
 
And using partial anonymous functions:<langsyntaxhighlight lang="quack">let multiply :- &(*)</langsyntaxhighlight>
 
=={{header|Quackery}}==
<langsyntaxhighlight lang="quackery">[ * ] is multiply ( n n --> n )</langsyntaxhighlight>
In the Quackery shell (REPL):
<pre>
Line 2,691 ⟶ 3,029:
 
Words don't have to be named. We could have written the above as:
<langsyntaxhighlight lang="quackery">2 ' [ * ] 3 swap do</langsyntaxhighlight>
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}}==
<langsyntaxhighlight lang="rsplus">mult <- function(a,b) a*b</langsyntaxhighlight>
In general:
<langsyntaxhighlight lang="rsplus">mult <- function(a,b) {
a*b
# or:
# return(a*b)
}</langsyntaxhighlight>
 
=={{header|Racket}}==
A simple function definition that takes 2 arguments.
 
<langsyntaxhighlight lang="racket">(define (multiply a b) (* a b))</langsyntaxhighlight>
 
Using an explicit <code>lambda</code> or <code>λ</code> is completely equivalent:
<langsyntaxhighlight lang="racket">(define multiply (lambda (a b) (* a b)))</langsyntaxhighlight>
 
<langsyntaxhighlight lang="racket">(define multiply (λ (a b) (* a b)))</langsyntaxhighlight>
 
Note that <code>*</code> is a function value, so the following code also works (although <code>multiply</code> will now be variadic function).
 
<syntaxhighlight lang ="racket">(define multiply *)</langsyntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
Without a signature:
<syntaxhighlight lang="raku" perl6line>sub multiply { return @_[0] * @_[1]; }</langsyntaxhighlight>
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:
<syntaxhighlight lang="raku" perl6line>sub multiply { [*] @_ }</langsyntaxhighlight>
With formal parameters and a return type:
<syntaxhighlight lang="raku" perl6line>sub multiply (Rat $a, Rat $b --> Rat) { $a * $b }</langsyntaxhighlight>
Same thing:
<syntaxhighlight lang="raku" perl6line>my Rat sub multiply (Rat $a, Rat $b) { $a * $b }</langsyntaxhighlight>
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:
<syntaxhighlight lang="raku" perl6line>my &multiply := -> $a, $b { $a * $b };</langsyntaxhighlight>
Another way to write a lambda is with internal placeholder parameters:
<syntaxhighlight lang="raku" perl6line>my &multiply := { $^a * $^b };</langsyntaxhighlight>
(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:
<syntaxhighlight lang="raku" perl6line>my &multiply := * * *;</langsyntaxhighlight>
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
curry a cascade of methods with only the original invocant missing:
<syntaxhighlight lang="raku" perl6line>@list.grep( *.substr(0,1).lc.match(/<[0..9 a..f]>/) )</langsyntaxhighlight>
This is equivalent to:
<syntaxhighlight lang="raku" perl6line>@list.grep( -> $obj { $obj.substr(0,1).lc.match(/<[0..9 a..f]>/) } )</langsyntaxhighlight>
 
=={{header|Raven}}==
<langsyntaxhighlight lang="raven">define multiply use a, b
a b *</langsyntaxhighlight>
Or optional infix:
<langsyntaxhighlight lang="raven">define multiply use a, b
(a * b)</langsyntaxhighlight>
Or skip named vars:
<syntaxhighlight lang ="raven">define multiply *</langsyntaxhighlight>
 
=={{header|REALbasic}}==
<lang vb>
Function Multiply(a As Integer, b As Integer) As Integer
Return a * b
End Function
</lang>
 
=={{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:
<langsyntaxhighlight REBOLlang="rebol">multiply: func [a b][a * b]</langsyntaxhighlight>
 
=={{header|Relation}}==
<syntaxhighlight lang="relation">
<lang Relation>
function multiply(a,b)
set result = a*b
end function
</syntaxhighlight>
</lang>
 
=={{header|Retro}}==
<langsyntaxhighlight Retrolang="retro">: multiply ( nn-n ) * ;</langsyntaxhighlight>
 
=={{header|REXX}}==
===exactitudeness===
<langsyntaxhighlight lang="rexx">multiply: return arg(1) * arg(2) /*return the product of the two arguments.*/</langsyntaxhighlight>
 
===cleaner display===
Line 2,781 ⟶ 3,112:
<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.
<langsyntaxhighlight lang="rexx">multiply: return arg(1) * arg(2) / 1 /*return with a normalized product of 2 args. */</langsyntaxhighlight>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
func multiply x,y return x*y
</syntaxhighlight>
</lang>
 
=={{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:
<langsyntaxhighlight RLaBlang="rlab">>> class(sin)
function
>> type(sin)
builtin</langsyntaxhighlight>
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
<langsyntaxhighlight RLaBlang="rlab">f = function(x, y)
{
return x + y;
Line 2,805 ⟶ 3,136:
function
>> type(f)
user</langsyntaxhighlight>
 
2. function can be member of a list (associative array)
<langsyntaxhighlight RLaBlang="rlab">somelist = <<>>;
somelist.f = function(x, y)
{
rval = x + y;
return rval;
};</langsyntaxhighlight>
 
3. user function which uses a function that is specified as a member of some list, here we use ''somelist'' from above:
<langsyntaxhighlight RLaBlang="rlab">g = function(x, y)
{
global(somelist);
rval = x * somelist.f(x, 2*y);
return rval;
};</langsyntaxhighlight>
 
=={{header|RPL}}==
≪ * ≫ 'MULT' STO
2 3 MULT
{{out}}
<pre>6</pre>
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def multiply(a, b)
a * b
end</langsyntaxhighlight>
Ruby 3.0 adds endless method definition:
<syntaxhighlight lang="ruby">def multiply(a, b) = a * b</syntaxhighlight>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn multiply(a: i32, b: i32) -> i32 {
a * b
}</langsyntaxhighlight>
 
=={{header|S-BASIC}}==
S-BASIC is unusual in that the function return value is assigned to the END statement that terminates the function.
<lang basic>
function multiply(a, b = real) = real
end = a * b
</lang>
 
=={{header|Sather}}==
<langsyntaxhighlight lang="sather">class MAIN is
-- we cannot have "functions" (methods) outside classes
mult(a, b:FLT):FLT is return a*b; end;
Line 2,848 ⟶ 3,179:
#OUT + mult(5.2, 3.4) + "\n";
end;
end;</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">def multiply(a: Int, b: Int) = a * b</langsyntaxhighlight>
 
=={{header|Scheme}}==
<syntaxhighlight lang ="scheme">(define multiply *)</langsyntaxhighlight>
Alternately,
<langsyntaxhighlight lang="scheme">(define (multiply a b)
(* a b))</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">const func float: multiply (in float: a, in float: b) is
return a * b;</langsyntaxhighlight>
 
=={{header|SenseTalk}}==
<langsyntaxhighlight lang="sensetalk">put multiply(3,7) as words
 
to multiply num1, num2
return num1 * num2
end multiply
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,876 ⟶ 3,207:
 
=={{header|SETL}}==
<langsyntaxhighlight lang="setl">proc multiply( a, b );
return a * b;
end proc;</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func multiply(a, b) {
a * b;
}</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="simula">BEGIN
INTEGER PROCEDURE multiply(x, y);
INTEGER x, y;
Line 2,895 ⟶ 3,226:
Outint(multiply(7,8), 2);
Outimage
END</langsyntaxhighlight>
 
=={{header|Slate}}==
<langsyntaxhighlight lang="slate">define: #multiply -> [| :a :b | a * b].</langsyntaxhighlight>
or using a macro:
<langsyntaxhighlight lang="slate">define: #multiply -> #* `er.</langsyntaxhighlight>
The block may also be installed as a method like so:
<langsyntaxhighlight lang="slate">a@(Number traits) multiplyBy: b@(Number traits) [a * b].</langsyntaxhighlight>
or more explicitly (without sugar):
<langsyntaxhighlight lang="slate">[| :a :b | a * b] asMethod: #multipleBy: on: {Number traits. Number traits}.</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
<langsyntaxhighlight lang="smalltalk">|mul|
mul := [ :a :b | a * b ].</langsyntaxhighlight>
 
=={{header|SNOBOL4}}==
<langsyntaxhighlight lang="snobol4"> define('multiply(a,b)') :(mul_end)
multiply multiply = a * b :(return)
mul_end
Line 2,917 ⟶ 3,248:
output = multiply(10.1,12.2)
output = multiply(10,12)
end</langsyntaxhighlight>
{{out}}
123.22
Line 2,924 ⟶ 3,255:
=={{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.
<langsyntaxhighlight lang="snusp">+1>++2=@\=>+++3=@\==@\=.=# prints '6'
| | \=itoa=@@@+@+++++#
\=======!\==!/===?\<#
\>+<-/</langsyntaxhighlight>
 
=={{header|SPARK}}==
The function definition (multiplies two standard Integer):
<langsyntaxhighlight Adalang="ada">package Functions is
function Multiply (A, B : Integer) return Integer;
--# pre A * B in Integer; -- See note below
--# return A * B; -- Implies commutativity on Multiply arguments
end Functions;</langsyntaxhighlight>
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 :
<langsyntaxhighlight Adalang="ada">type Input_Type is range 0 .. 10;
type Result_Type is range 0 .. 100;</langsyntaxhighlight>
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:
<langsyntaxhighlight Adalang="ada">package body Functions is
function Multiply (A, B : Integer) return Integer is
begin
return A * B;
end Multiply;
end Functions;</langsyntaxhighlight>
 
=={{header|SPL}}==
Single-line function definition:
<langsyntaxhighlight lang="spl">multiply(a,b) <= a*b</langsyntaxhighlight>
Multi-line function definition:
<langsyntaxhighlight lang="spl">multiply(a,b)=
x = a*b
<= x
.</langsyntaxhighlight>
 
=={{header|SSEM}}==
Line 2,962 ⟶ 3,293:
 
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:
<langsyntaxhighlight lang="ssem">01000000000000100000000000000000 0. -2 to c
00100000000000000000000000000000 1. 4 to CI
01111111111111111111111111111111 2. -2
00000000000001110000000000000000 3. Stop
11100000000000000000000000000000 4. 7</langsyntaxhighlight>
or in pseudocode:
<pre> load &here
Line 2,972 ⟶ 3,303:
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>.
<langsyntaxhighlight lang="ssem">00010000000000000000000000000000 6. 8
11100000000000000000000000000000 7. 7
11111000000001100000000000000000 8. c to 31
Line 2,997 ⟶ 3,328:
00110000000000000000000000000000 29. 12
00000000000000000000000000000000 30. 0
00000000000000000000000000000000 31. 0</langsyntaxhighlight>
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
Line 3,018 ⟶ 3,349:
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="ocaml">val multiply = op *</langsyntaxhighlight>
Equivalently,
<langsyntaxhighlight lang="ocaml">fun multiply (x, y) = x * y</langsyntaxhighlight>
Using lambda syntax:
<langsyntaxhighlight lang="sml">val multiply = fn (x, y) => x * y</langsyntaxhighlight>
Curried form:
<langsyntaxhighlight lang="ocaml">fun multiply x y = x * y</langsyntaxhighlight>
 
=={{header|Stata}}==
Line 3,031 ⟶ 3,362:
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.
 
<langsyntaxhighlight lang="stata">prog def multiply, return
args a b
return sca product=`a'*`b'
Line 3,037 ⟶ 3,368:
 
multiply 77 13
di r(product)</langsyntaxhighlight>
 
'''Output'''
Line 3,046 ⟶ 3,377:
Mata is the matrix language of Stata. Here is how to define a function
 
<langsyntaxhighlight lang="stata">mata
scalar multiply(scalar x, scalar y) {
return(x*y)
Line 3,052 ⟶ 3,383:
 
multiply(77,13)
end</langsyntaxhighlight>
 
'''Output'''
Line 3,059 ⟶ 3,390:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">func multiply(a: Double, b: Double) -> Double {
return a * b
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
Strictly as described in the task:
<langsyntaxhighlight lang="tcl">proc multiply { arg1 arg2 } {
return [expr {$arg1 * $arg2}]
}</langsyntaxhighlight>
{{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):
<langsyntaxhighlight lang="tcl">proc tcl::mathfunc::multiply {arg1 arg2} {
return [expr {$arg1 * $arg2}]
}
Line 3,077 ⟶ 3,408:
if {multiply(6, 9) == 42} {
puts "Welcome, Citizens of Golgafrincham from the B-Ark!"
}</langsyntaxhighlight>
 
=={{header|TI-89 BASIC}}==
<lang ti89b>multiply(a, b)
Func
Return a * b
EndFunc</lang>
 
=={{header|Toka}}==
<langsyntaxhighlight lang="toka">[ ( ab-c ) * ] is multiply</langsyntaxhighlight>
 
=={{header|Transd}}==
<langsyntaxhighlight lang="scheme">multiply: (lambda a Double() b Double() (* a b))</langsyntaxhighlight>
 
=={{header|TXR}}==
Line 3,095 ⟶ 3,420:
 
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>
<langsyntaxhighlight lang="txr">@(define multiply (a b out))
@(bind out @(* a b))
@(end)
@(multiply 3 4 result)</langsyntaxhighlight>
<pre>$ txr -B multiply.txr
result="12"</pre>
In the embedded Lisp dialect, it is possible to write an ordinary function that returns a value:
<langsyntaxhighlight lang="txrlisp">(defun mult (a b) (* a b))
(put-line `3 * 4 = @(mult 3 4)`)</langsyntaxhighlight>
<pre>$ txr multiply.tl
3 * 4 = 12</pre>
 
=={{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.
<lang>PRINT FUNC (_Multiply (2,3))
END
 
_Multiply PARAM (2)
RETURN (a@ * b@)</lang>
 
=={{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.
{{works with|Bourne Shell}}
<langsyntaxhighlight lang="bash">multiply() {
# There is never anything between the parentheses after the function name
# Arguments are obtained using the positional parameters $1, and $2
Line 3,127 ⟶ 3,444:
# Call the function
multiply 3 4 # The function is invoked in statement context
echo $? # The dollarhook special variable gives the return value</langsyntaxhighlight>
{{works with|Bash}}
return an exit code
<langsyntaxhighlight lang="bash">multiply() {
return $(($1 * $2))
}
multiply 5 6
echo $?</langsyntaxhighlight>
echo the result
<langsyntaxhighlight lang="bash">multiply() {
echo -n $(($1 * $2))
}
echo $(multiply 5 6)</langsyntaxhighlight>
 
=={{header|Ursa}}==
<langsyntaxhighlight lang="ursa"># multiply is a built-in in ursa, so the function is called mult instead
def mult (int a, int b)
return (* a b)
end</langsyntaxhighlight>
 
=={{header|Ursala}}==
Line 3,153 ⟶ 3,470:
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,
<langsyntaxhighlight Ursalalang="ursala">multiply = math..mul</langsyntaxhighlight>
this is the definition using lambda abstraction
<langsyntaxhighlight Ursalalang="ursala">multiply = ("a","b"). math..mul ("a","b")</langsyntaxhighlight>
and this is the definition using pattern matching.
<langsyntaxhighlight Ursalalang="ursala">multiply("a","b") = math..mul ("a","b")</langsyntaxhighlight>
 
=={{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.
<syntaxhighlight lang ="v">[multiply *].</langsyntaxhighlight>
Using it
<langsyntaxhighlight lang="v">2 3 multiply
=6</langsyntaxhighlight>
V also allows internal bindings.
<langsyntaxhighlight lang="v">[multiply
[a b] let
a b *].</langsyntaxhighlight>
 
=={{header|VBAV (Vlang)}}==
<syntaxhighlight lang="Zig">
<lang vb>Function Multiply(lngMcand As Long, lngMplier As Long) As Long
fn multiply(a f64, b f64) f64 {
Multiply = lngMcand * lngMplier
return a * b
End Function</lang>
}
To use this function :
<lang vb>Sub Main()
Dim Result As Long
Result = Multiply(564231, 897)
End Sub</lang>
 
fn main() {
=={{header|VBScript}}==
<lang vb>function print(multiply( multiplicand5, multiplier 6))
}
multiply = multiplicand * multiplier
</syntaxhighlight>
end function</lang>
Usage:
<lang vb>dim twosquared
twosquared = multiply(2, 2)</lang>
 
{{out}}
=={{header|Visual Basic}}==
<pre>
{{works with|Visual Basic|VB6 Standard}}
30.0
<lang vb>
</pre>
Function multiply(a As Integer, b As Integer) As Integer
multiply = a * b
End Function
</lang>
Call the function
<lang vb>Multiply(6, 111)</lang>
 
=={{header|Visual Basic .NET}}==
<lang vbnet>Function Multiply(ByVal a As Integer, ByVal b As Integer) As Integer
Return a * b
End Function</lang>
Call the function
<lang vbnet>Multiply(1, 1)</lang>
 
=={{header|Wart}}==
A straightforward way to say how calls of the form <code>(multiply a b)</code> are translated:
<langsyntaxhighlight lang="python">def (multiply a b)
a*b</langsyntaxhighlight>
<langsyntaxhighlight lang="python">(multiply 3 4)
=> 12</langsyntaxhighlight>
Functions can also use keyword args.
<langsyntaxhighlight lang="python">(multiply 3 :a 4) # arg order doesn't matter here, but try subtract instead
=> 12</langsyntaxhighlight>
Finally, we can give parameters better keyword args using <em>aliases</em>:
<langsyntaxhighlight lang="python">def (multiply a b|by)
(* a b)</langsyntaxhighlight>
<langsyntaxhighlight lang="python">multiply 3 :by 4
=> 12</langsyntaxhighlight>
 
=={{header|WebAssembly}}==
Line 3,237 ⟶ 3,535:
The following 'multiply' function will work for any type(s) that support the '*' operator.
However, it will produce a runtime error otherwise, as demonstrated by the final example.
<langsyntaxhighlight ecmascriptlang="wren">var multiply = Fn.new { |a, b| a * b }
 
System.print(multiply.call(3, 7))
System.print(multiply.call("abc", 3))
System.print(multiply.call([1], 5))
System.print(multiply.call(true, false))</langsyntaxhighlight>
 
{{out}}
Line 3,268 ⟶ 3,566:
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>.
 
<langsyntaxhighlight lang="asm"> .text
.globl multiply
.type multiply,@function
Line 3,274 ⟶ 3,572:
movl 4(%esp), %eax
mull 8(%esp)
ret</langsyntaxhighlight>
 
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,280 ⟶ 3,578:
===NASM===
{{works with|NASM}}
<langsyntaxhighlight lang="asm">section .text
global _start
Line 3,303 ⟶ 3,601:
push 6
push 16
call _multiply_stack</langsyntaxhighlight>
 
===MASM===
However, in MASM we do have function statements due to the preprocessor.
{{works with|MASM}}
<langsyntaxhighlight lang="asm">multiply proc arg1:dword, arg2:dword
mov eax, arg1
mov ebx, arg2
Line 3,314 ⟶ 3,612:
mov eax, ebx
ret
multiply endp</langsyntaxhighlight>
Then to call it.
<langsyntaxhighlight lang="asm">invoke multiply, 6, 16
;or..
push 16
push 6
call multiply</langsyntaxhighlight>
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}}==
Functions are defined by using the '''func''' keyword.
<langsyntaxhighlight XBSlang="xbs">func multiply(a,b){
send a*b;
}</langsyntaxhighlight>
 
=={{header|XLISP}}==
Functions can be defined using either 'classic' Lisp syntax:
<langsyntaxhighlight lang="lisp">(defun multiply (x y)
(* x y))</langsyntaxhighlight>
or Scheme-style syntax:
<langsyntaxhighlight lang="scheme">(define (multiply x y)
(* x y))</langsyntaxhighlight>
or, if you prefer, with <tt>LAMBDA</tt>:
<langsyntaxhighlight lang="scheme">(define multiply
(lambda (x y) (* x y)))</langsyntaxhighlight>
 
=={{header|Xojo}}==
<lang vbnet>Function Multiply(ByVal a As Integer, ByVal b As Integer) As Integer
Return a * b
End Function</lang>
Call the function
<lang vbnet>Dim I As Integer = Multiply(7, 6)</lang>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func Multiply(A, B); \the characters in parentheses are only a comment
int A, B; \the arguments are actually declared here, as integers
return A*B; \the default (undeclared) function type is integer
Line 3,355 ⟶ 3,646:
func real FloatMul(A, B); \floating point version
real A, B; \arguments are declared here as floating point (doubles)
return A*B;</langsyntaxhighlight>
 
=={{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).
<langsyntaxhighlight lang="xslt"><xsl:template name="multiply">
<xsl:param name="a" select="2"/>
<xsl:param name="b" select="3"/>
<xsl:value-of select="$a * $b"/>
</xsl:template></langsyntaxhighlight>
Usage examples.
<langsyntaxhighlight lang="xslt"><xsl:call-template name="multiply">
<xsl:with-param name="a">4</xsl:with-param>
<xsl:with-param name="b">5</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="multiply"/> <-- using default parameters of 2 and 3 --></langsyntaxhighlight>
 
Available in XSLT 2.0 and later versions.
<langsyntaxhighlight lang="xslt"><xsl:function name="mf:multiply">
<xsl:param name="a"/>
<xsl:param name="b"/>
<xsl:value-of select="$a * $b"/>
</xsl:function></langsyntaxhighlight>
Usage examples.
<langsyntaxhighlight lang="xslt">{mf:multiply(2,3)}
<xsl:value-of select="mf:multiply(2,3)" /></langsyntaxhighlight>
 
=={{header|Yorick}}==
<langsyntaxhighlight lang="yorick">func multiply(x, y) {
return x * y;
}</langsyntaxhighlight>
Example of interactive usage:
<pre>> multiply(2, 4.5)
Line 3,393 ⟶ 3,684:
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.
 
<langsyntaxhighlight lang="z80">doMultiply:
;returns HL = HL times A. No overflow protection.
push bc
Line 3,419 ⟶ 3,710:
pop de
pop bc
ret</langsyntaxhighlight>
 
=={{header|zklzig}}==
<langsyntaxhighlight zkllang="zig">fcnfun multiply(x: i64, y: i64) i64 {x*y}</lang>
return x * y;
<lang zkl>fcn(x,y){x*y}(4.5,3) // --> 13.5</lang>
}
Since all functions are vararg:<lang zkl>fcn multiply{vm.arglist.reduce('*)}
multiply(1,2,3,4,5) //--> 120</lang>
Operators are first class objects so:<lang zkl>var mul=Op("*"); mul(4,5) //-->20</lang>
 
//example call
=={{header|ZX Spectrum Basic}}==
const x: i64 = 4;
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
const y: i64 = 23;
<lang zxbasic>10 PRINT FN m(3,4): REM call our function to produce a value of 12
_ = multipy(x, y); // --> 93</syntaxhighlight>
20 STOP
 
9950 DEF FN m(a,b)=a*b</lang>
=={{header|zkl}}==
<syntaxhighlight lang="zkl">fcn multiply(x,y){x*y}</syntaxhighlight>
<syntaxhighlight lang="zkl">fcn(x,y){x*y}(4.5,3) // --> 13.5</syntaxhighlight>
Since all functions are vararg:<syntaxhighlight lang="zkl">fcn multiply{vm.arglist.reduce('*)}
multiply(1,2,3,4,5) //--> 120</syntaxhighlight>
Operators are first class objects so:<syntaxhighlight lang="zkl">var mul=Op("*"); mul(4,5) //-->20</syntaxhighlight>
{{omit from|GUISS}}
885

edits