Function definition: Difference between revisions

m (syntax highlighting fixup automation)
(37 intermediate revisions by 22 users not shown)
Line 295:
MULTIPLY := A * B;
END;</syntaxhighlight>
 
=={{header|Amazing Hopper}}==
Hopper has no functions, but they can be declared with macros, which are resolved at compile time. Access to the working stack is global, but "local" variables can be declared in program segments written after the ".locals" clause. Let's look at some examples of declaring "functions".
<syntaxhighlight lang="c">
/* this need data into stack */
#context Multiplication
mul
Return \\
#synon Multiplication *getproduct
 
#context-free anothermul
/* #defn Args(*) #GENCODE $$$*$$$ #REVLIST=0,mov(#REVLIST);#ENDGEN, */
Args 'a,b'
Return ( #(a*b) )\\
#synon anothermul *getanotherproduct
 
#include <jambo.h>
 
#prototype _multiply(_X_,_Y_)
#synon __multiply Multiply
 
Main
/* "prototipos" of functions and procedures.
Solves internaly */
Printnl ( Multiply ( 10, 4 ) )
Printnl ( __multiply ( 10, 4 ) )
/* definición alternativa 1 */
Printnl ( Set' 10,4 ', Gosub ' Multiply2 ')
/* aseembler Hopper 1 */
{10,4} jsub( Multiply3 ), {"\n"} print
/* assembler Hopper 2 */
{10,4} jsub( Multiply4 ), {"\n"} print
/* context */
Set '10,4', now get product, and print with newline
/* context-free */
Set '10,4', and get another product; then print with newline
End
 
.locals /* Subrutines */
 
_multiply(a,b)
Return ( Mul(a,b) )
 
/* Define is macro. Others macros: Function, Procedure:
#defn Define(_F_,*) _F_:,#GENCODE $$$*$$$ #REVLIST=0;mov(#REVLIST);#ENDGEN;
#defn Function(_F_,*) _F_:,#GENCODE $$$*$$$ #REVLIST=0;mov(#REVLIST);#ENDGEN;
#defn Procedure(_F_,*) _F_:,#GENCODE $$$*$$$ #REVLIST=0;mov(#REVLIST);#ENDGEN;
*/
Define 'Multiply2, a,b'
Return ( Mul(a,b) )
 
Multiply3:
b=0, mov(b), a=0, mov(a)
{a,b}mul /* result into stack */
Return
 
Multiply4:
mul /* get values from stack,
and put result into stack */
back /* Return */
</syntaxhighlight>
{{out}}
<pre>
40.000000
40.000000
40.000000
40.000000
40.000000
40.000000
40.000000
</pre>
 
=={{header|AmigaE}}==
Line 342 ⟶ 419:
end</syntaxhighlight>
 
A function in AppleScript is called a "handler". It can take one of three different forms, depending on what the scripter finds most convenient. Calls to it must match the form used in the handler definition. 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.
 
<syntaxhighlight lang="applescript">on multiply(a, b)
Line 350 ⟶ 429:
multiply(2, 3)</syntaxhighlight>
 
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.
 
Example:
 
<syntaxhighlight lang="applescript">on multiplication of a by b
Line 367 ⟶ 450:
 
my multiply:2 |by|:3</syntaxhighlight>
 
=={{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.
 
<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|Argile}}==
Line 589 ⟶ 662:
 
=={{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.
<syntaxhighlight 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</syntaxhighlight>
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}}===
Line 600 ⟶ 701:
return a * b
end function</syntaxhighlight>
 
==={{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}}===
Line 607 ⟶ 745:
30 PRINT FN MULT(3)</syntaxhighlight>
 
==={{header|IS-BASICCreative Basic}}===
<syntaxhighlight lang="is-creative basic">100 DEF MULTIPLY(A,B)=A*B</syntaxhighlight>
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}}
<syntaxhighlight lang="basic">10 DEF FNMULT(X,Y)=X*Y
Line 616 ⟶ 827:
39 END
</syntaxhighlight>
 
==={{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}}===
Line 636 ⟶ 969:
print multiply(pi,2) '6.28...
</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">Procedure multiply(a,b)
ProcedureReturn a*b
EndProcedure</syntaxhighlight>
 
==={{header|QBasic}}===
Line 652 ⟶ 990:
{{out}}
<pre> 3.703680038452148</pre>
 
==={{header|QuickBASIC}}===
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">DECLARE FUNCTION multiply% (a AS INTEGER, b AS INTEGER)
 
FUNCTION multiply% (a AS INTEGER, b AS INTEGER)
multiply = a * b
END FUNCTION</syntaxhighlight>
 
==={{header|REALbasic}}===
<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}}===
Line 664 ⟶ 1,034:
 
END</syntaxhighlight>
 
==={{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.
<syntaxhighlight lang="text">Print FUNC(_multiply (23, 65))
End
 
_multiply Param (2) : Return (a@ * b@)</syntaxhighlight>
 
==={{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}}===
Line 675 ⟶ 1,087:
return a * b
end sub</syntaxhighlight>
 
==={{header|Xojo}}===
<syntaxhighlight lang="vbnet">Function Multiply(ByVal a As Integer, ByVal b As Integer) As Integer
Return a * b
End Function</syntaxhighlight>
Call the function
<syntaxhighlight lang="vbnet">Dim I As Integer = Multiply(7, 6)</syntaxhighlight>
 
==={{header|ZX Spectrum Basic}}===
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}}==
Line 689 ⟶ 1,114:
 
:eof</syntaxhighlight>
 
=={{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|bc}}==
Line 738 ⟶ 1,149:
 
print multiply(3, 2)</syntaxhighlight>
 
=={{header|Binary Lambda Calculus}}==
 
In lambda calculus, multiplication on Church numerals is <code>mul = \m \n \f. m (n f)</code> which in BLC is
 
<pre>00 00 00 01 1110 01 110 10</pre>
 
If mul is used several times within an expression E, then they can share the same definition by using <code>(\mul. E)(\m\n\f. m (n f))</code>. For example, the cube function is <code>\n. (\mul. mul n (mul n n)) (\m\n\f. m (n f))</code> which in BLC is
 
<pre>00 01 00 01 01 10 110 01 01 10 110 110 0000000111100111010</pre>
 
=={{header|BQN}}==
Line 846 ⟶ 1,267:
 
=={{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:
<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.</syntaxhighlight>
 
{{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}}
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. myTest.
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.</syntaxhighlight>
 
Line 918 ⟶ 1,340:
 
=={{header|ColdFusion}}==
====Tag style====
<syntaxhighlight lang="coldfusion"><cffunction name="multiply" returntype="numeric">
<cfargument name="a" type="numeric">
Line 923 ⟶ 1,346:
<cfreturn a * b>
</cffunction></syntaxhighlight>
 
====Script style====
 
<syntaxhighlight lang="lisp">numeric function multiply(required numeric a, required numeric b){
return a * b;
}
</syntaxhighlight>
 
=={{header|Common Lisp}}==
Line 978 ⟶ 1,408:
rslt := a * b;
end sub</syntaxhighlight>
 
=={{header|Creative Basic}}==
<syntaxhighlight 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.
</syntaxhighlight>
 
=={{header|D}}==
Line 1,080 ⟶ 1,479:
 
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,125 ⟶ 1,530:
 
=={{header|EasyLang}}==
<syntaxhighlight lang="text">func multiply a b . r .
func r =multiply a *b b.
return a * b
.
callprint multiply 7 5 res
print res</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 1,159 ⟶ 1,565:
multiply → (λ (_a _b) (#🔶_multiply)) ;; compiled function
</syntaxhighlight>
 
=={{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}}==
Line 1,187 ⟶ 1,616:
= a * b;</syntaxhighlight>
Anonymous function / closure:
<syntaxhighlight lang="elena">symbol f := (x,y => x * y);</syntaxhighlight>
Root closure:
<syntaxhighlight lang="elena">f(x,y){ ^ x * y }</syntaxhighlight>
Line 1,227 ⟶ 1,656:
"Return the product of X and Y."
(* x y))</syntaxhighlight>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
fun multiply = var by var a, var b
return a * b
end
writeLine(multiply(6, 7))
writeLine(multiply("can", 2))
</syntaxhighlight>
{{out}}
<pre>
42
cancan
</pre>
 
=={{header|Erlang}}==
Line 1,406 ⟶ 1,849:
end function multiply
end module elemFunc</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|Free Pascal}}==
Line 1,444 ⟶ 1,874:
let multiply (x: i32, y: i32) : i32 = x * y
</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|GAP}}==
Line 1,604 ⟶ 2,002:
=={{header|Io}}==
<syntaxhighlight lang="io">multiply := method(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|J}}==
Line 1,854 ⟶ 2,151:
</syntaxhighlight>
 
=={{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.
<syntaxhighlight 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)</syntaxhighlight>
}
</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.
<syntaxhighlight lang="langur">val .multiply = f fn(.x, .y) { .x * .y }
.multiply(3, 4)</syntaxhighlight>
 
=== 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}}
<syntaxhighlight lang="langur">val .multiply = f{x}
.multiply(3, 4)</syntaxhighlight>
 
=== 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}}
<syntaxhighlight lang="langur">val .times3 = f{x 3}
map .times3, [1, 2, 3]</syntaxhighlight>
 
=== 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,924 ⟶ 2,247:
multiply call (2, 3).
multiply call: 2, 3.</syntaxhighlight>
 
=={{header|LDPL}}==
<syntaxhighlight lang="ldpl">data:
n is number
 
procedure:
sub multiply
parameters:
x is number
y is number
result is number
procedure:
in result solve x * y
end sub
 
# call the bare sub-procedure
call multiply with 3 4 n
display n lf
 
# create a statement for it
create statement "multiply $ by $ in $" executing multiply
 
multiply 3 by 4 in n
display n lf
</syntaxhighlight>
{{out}}
<pre>
12
12
</pre>
 
=={{header|LFE}}==
Line 1,930 ⟶ 2,283:
(* a b))
</syntaxhighlight>
 
=={{header|Liberty 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|Lily}}==
Line 1,962 ⟶ 2,302:
 
put multiplyy(2,5) -- = 10</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|Logo}}==
Line 2,640 ⟶ 2,975:
A is 5*2,
format('The product is ~d.~n', [A]).</syntaxhighlight>
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">Procedure multiply(a,b)
ProcedureReturn a*b
EndProcedure</syntaxhighlight>
 
=={{header|Python}}==
Line 2,759 ⟶ 3,089:
Or skip named vars:
<syntaxhighlight lang="raven">define multiply *</syntaxhighlight>
 
=={{header|REALbasic}}==
<syntaxhighlight lang="vb">
Function Multiply(a As Integer, b As Integer) As Integer
Return a * b
End Function
</syntaxhighlight>
 
=={{header|REBOL}}==
Line 2,831 ⟶ 3,154:
};</syntaxhighlight>
 
=={{header|RPL}}==
≪ * ≫ 'MULT' STO
2 3 MULT
{{out}}
<pre>6</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">def multiply(a, b)
a * b
end</syntaxhighlight>
Ruby 3.0 adds endless method definition:
<syntaxhighlight lang="ruby">def multiply(a, b) = a * b</syntaxhighlight>
 
=={{header|Rust}}==
Line 2,840 ⟶ 3,170:
a * b
}</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 = real) = real
end = a * b
</syntaxhighlight>
 
=={{header|Sather}}==
Line 3,086 ⟶ 3,409:
puts "Welcome, Citizens of Golgafrincham from the B-Ark!"
}</syntaxhighlight>
 
=={{header|TI-89 BASIC}}==
<syntaxhighlight lang="ti89b">multiply(a, b)
Func
Return a * b
EndFunc</syntaxhighlight>
 
=={{header|Toka}}==
Line 3,114 ⟶ 3,431:
<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.
<syntaxhighlight lang="text">PRINT FUNC (_Multiply (2,3))
END
 
_Multiply PARAM (2)
RETURN (a@ * b@)</syntaxhighlight>
 
=={{header|UNIX Shell}}==
Line 3,178 ⟶ 3,487:
a b *].</syntaxhighlight>
 
=={{header|VBAV (Vlang)}}==
<syntaxhighlight lang="vbZig">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</syntaxhighlight>
}
To use this function :
<syntaxhighlight lang="vb">Sub Main()
Dim Result As Long
Result = Multiply(564231, 897)
End Sub</syntaxhighlight>
 
fn main() {
=={{header|VBScript}}==
print(multiply(5, 6))
<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>
 
{{out}}
=={{header|Visual Basic .NET}}==
<pre>
<syntaxhighlight lang="vbnet">Function Multiply(ByVal a As Integer, ByVal b As Integer) As Integer
30.0
Return a * b
</pre>
End Function</syntaxhighlight>
Call the function
<syntaxhighlight lang="vbnet">Multiply(1, 1)</syntaxhighlight>
 
=={{header|Wart}}==
Line 3,245 ⟶ 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.
<syntaxhighlight lang="ecmascriptwren">var multiply = Fn.new { |a, b| a * b }
 
System.print(multiply.call(3, 7))
Line 3,347 ⟶ 3,637:
<syntaxhighlight lang="scheme">(define multiply
(lambda (x y) (* x y)))</syntaxhighlight>
 
=={{header|Xojo}}==
<syntaxhighlight lang="vbnet">Function Multiply(ByVal a As Integer, ByVal b As Integer) As Integer
Return a * b
End Function</syntaxhighlight>
Call the function
<syntaxhighlight lang="vbnet">Dim I As Integer = Multiply(7, 6)</syntaxhighlight>
 
=={{header|XPL0}}==
Line 3,428 ⟶ 3,711:
pop bc
ret</syntaxhighlight>
 
=={{header|zig}}==
<syntaxhighlight lang="zig">fun multiply(x: i64, y: i64) i64 {
return x * y;
}
 
//example call
const x: i64 = 4;
const y: i64 = 23;
_ = multipy(x, y); // --> 93</syntaxhighlight>
 
=={{header|zkl}}==
Line 3,435 ⟶ 3,728:
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>
 
=={{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>
{{omit from|GUISS}}
885

edits