Function definition: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
(Dialects of BASIC moved to the BASIC section (uBASIC was twice - merged).)
Line 367: Line 367:


my multiply:2 |by|:3</syntaxhighlight>
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}}==
=={{header|Argile}}==
Line 595: Line 585:
multiply = a * b
multiply = a * b
END FUNCTION</syntaxhighlight>
END FUNCTION</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|BASIC256}}===
==={{header|BASIC256}}===
Line 600: Line 600:
return a * b
return a * b
end function</syntaxhighlight>
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|Commodore BASIC}}===
==={{header|Commodore BASIC}}===
Line 607: Line 621:
30 PRINT FN MULT(3)</syntaxhighlight>
30 PRINT FN MULT(3)</syntaxhighlight>


==={{header|IS-BASIC}}===
==={{header|Creative Basic}}===
<syntaxhighlight lang="is-basic">100 DEF MULTIPLY(A,B)=A*B</syntaxhighlight>
<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|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|GWBASIC}}===
==={{header|GW-BASIC}}===
{{works with|BASICA}}
{{works with|BASICA}}
<syntaxhighlight lang="basic">10 DEF FNMULT(X,Y)=X*Y
<syntaxhighlight lang="basic">10 DEF FNMULT(X,Y)=X*Y
Line 616: Line 703:
39 END
39 END
</syntaxhighlight>
</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}}===
==={{header|OxygenBasic}}===
Line 636: Line 845:
print multiply(pi,2) '6.28...
print multiply(pi,2) '6.28...
</syntaxhighlight>
</syntaxhighlight>

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


==={{header|QBasic}}===
==={{header|QBasic}}===
Line 652: Line 866:
{{out}}
{{out}}
<pre> 3.703680038452148</pre>
<pre> 3.703680038452148</pre>

==={{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 = real) = real
end = a * b
</syntaxhighlight>


==={{header|True BASIC}}===
==={{header|True BASIC}}===
Line 664: Line 892:


END</syntaxhighlight>
END</syntaxhighlight>

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


==={{header|uBasic/4tH}}===
==={{header|uBasic/4tH}}===
In uBasic you can turn any subroutine into a function with the '''FUNC()''' function. It takes one argument, which is the label. Arguments are optional.
<syntaxhighlight lang="text">Print FUNC(_multiply (23, 65))
<syntaxhighlight lang="text">Print FUNC(_multiply (23, 65))
End
End


_multiply Param (2) : Return (a@ * b@)</syntaxhighlight>
_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}}===
==={{header|Yabasic}}===
Line 675: Line 945:
return a * b
return a * b
end sub</syntaxhighlight>
end sub</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}}==
=={{header|Batch File}}==
Line 689: Line 965:


:eof</syntaxhighlight>
: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}}==
=={{header|bc}}==
Line 978: Line 1,240:
rslt := a * b;
rslt := a * b;
end sub</syntaxhighlight>
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}}==
=={{header|D}}==
Line 1,406: Line 1,637:
end function multiply
end function multiply
end module elemFunc</syntaxhighlight>
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}}==
=={{header|Free Pascal}}==
Line 1,444: Line 1,662:
let multiply (x: i32, y: i32) : i32 = x * y
let multiply (x: i32, y: i32) : i32 = x * y
</syntaxhighlight>
</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}}==
=={{header|GAP}}==
Line 1,604: Line 1,790:
=={{header|Io}}==
=={{header|Io}}==
<syntaxhighlight lang="io">multiply := method(a,b,a*b)</syntaxhighlight>
<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}}==
=={{header|J}}==
Line 1,930: Line 2,015:
(* a b))
(* a b))
</syntaxhighlight>
</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}}==
=={{header|Lily}}==
Line 1,962: Line 2,034:


put multiplyy(2,5) -- = 10</syntaxhighlight>
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}}==
=={{header|Logo}}==
Line 2,640: Line 2,707:
A is 5*2,
A is 5*2,
format('The product is ~d.~n', [A]).</syntaxhighlight>
format('The product is ~d.~n', [A]).</syntaxhighlight>

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


=={{header|Python}}==
=={{header|Python}}==
Line 2,759: Line 2,821:
Or skip named vars:
Or skip named vars:
<syntaxhighlight lang="raven">define multiply *</syntaxhighlight>
<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}}==
=={{header|REBOL}}==
Line 2,840: Line 2,895:
a * b
a * b
}</syntaxhighlight>
}</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}}==
=={{header|Sather}}==
Line 3,086: Line 3,134:
puts "Welcome, Citizens of Golgafrincham from the B-Ark!"
puts "Welcome, Citizens of Golgafrincham from the B-Ark!"
}</syntaxhighlight>
}</syntaxhighlight>

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


=={{header|Toka}}==
=={{header|Toka}}==
Line 3,114: Line 3,156:
<pre>$ txr multiply.tl
<pre>$ txr multiply.tl
3 * 4 = 12</pre>
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}}==
=={{header|UNIX Shell}}==
Line 3,177: Line 3,211:
[a b] let
[a b] let
a b *].</syntaxhighlight>
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|Wart}}==
=={{header|Wart}}==
Line 3,435: Line 3,434:
multiply(1,2,3,4,5) //--> 120</syntaxhighlight>
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>
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}}
{{omit from|GUISS}}