Function definition: Difference between revisions

Add SmallBASIC
(→‎{{header|BASIC}}: Added ANSI BASIC.)
(Add SmallBASIC)
 
(6 intermediate revisions by 3 users not shown)
Line 1,022:
The product of 9 times 3 is 27
</pre>
 
==={{header|SmallBASIC}}===
<syntaxhighlight lang="langurqbasic">val .multiply = fn{*}
func multiply(a, b)
return a * b
end
 
print "2 * 3 = "; multiply(2, 3)
.multiply(3, 4)</syntaxhighlight>
 
If function definition is only one line, <code>def</code> can be used:
<syntaxhighlight lang="langurqbasic">val .times3 = fn{* 3}
def multiply(a, b) = a * b
print "2 * 3 = "; multiply(2, 3)
.multiply(3, 4)</syntaxhighlight>
 
 
==={{header|True BASIC}}===
Line 1,219 ⟶ 1,235:
return a*b;
}</syntaxhighlight>
 
=={{header|Chapel}}==
 
<syntaxhighlight lang="text">
 
proc multiply(a, b)
{
return a * b;
}
.multiply(3, 4)</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)
}
map .times3, [1, 2, 3]</syntaxhighlight>
Will work on any type where the * operator is defined.
 
=={{header|ChucK}}==
Line 2,183 ⟶ 2,218:
 
=={{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,825 ⟶ 2,840:
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}}==
29

edits