Function definition: Difference between revisions

→‎{{header|langur}}: modified to suit task description
(→‎{{header|langur}}: modified to suit task description)
(7 intermediate revisions by 4 users not shown)
Line 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">
FUNCTION multiply% (a AS INTEGER, b AS INTEGER)
100 DEF Multiply(A, multiplyB) = aA * bB
END110 DECLARE FUNCTION</syntaxhighlight> 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}}===
Line 770 ⟶ 788:
 
<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}}===
Line 958 ⟶ 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}}===
Line 1,047 ⟶ 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}}===
Line 1,172 ⟶ 1,219:
return a*b;
}</syntaxhighlight>
 
=={{header|Chapel}}==
 
<syntaxhighlight lang="text">
 
proc multiply(a, b)
{
return a * b;
}
</syntaxhighlight>
 
Can require that the two arguments be of the same type.
<syntaxhighlight lang="text">
proc multiply(a : ?t ... 2)
{
return a(0) * a(1)
}
</syntaxhighlight>
Will work on any type where the * operator is defined.
 
=={{header|ChucK}}==
Line 1,827 ⟶ 1,893:
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|GAP}}==
Line 2,150 ⟶ 2,202:
 
=={{header|langur}}==
<syntaxhighlight>
Langur functions are first-order. They are pure in terms of setting values and in terms of I/O (unless declared impure).
val multiply = fn{*}
</syntaxhighlight>
 
<syntaxhighlight>
A return statement may be used, but a function's last value is its implicit return value.
val multiply = fn a, b: a * b
</syntaxhighlight>
 
<syntaxhighlight>
=== parameters ===
val multiply = fn(a, b) { a * b }
Parameters are defined within parentheses after the fn token. To specify no parameters, use an empty set of parentheses.
</syntaxhighlight lang="langur">val .multiply = fn(.x, .y) { .x * .y }
.multiply(3, 4)</syntaxhighlight>
 
=== curly braces ===
A function body may use curly braces, but it is not required if it is a single expression.
<syntaxhighlight lang="langur">val .multiply = 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 fn token.
 
<syntaxhighlight lang="langur">val .multiply = fn{*}
.multiply(3, 4)</syntaxhighlight>
 
=== nil left partially implied functions ===
These are built with an infix operator and a right-hand operand inside the fn{...} tokens.
 
<syntaxhighlight lang="langur">val .times3 = fn{* 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 2,792 ⟶ 2,824:
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.
 
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
function multiply(a,b: real): real := a + b;
</syntaxhighlight>
 
=={{header|Perl}}==
Line 3,609 ⟶ 3,646:
<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}}==
1,007

edits