Function definition: Difference between revisions

Content added Content deleted
(Add Brat solution)
m (Semi-automated edit: Bash -> UNIX Shell)
Line 94: Line 94:
print mul(5, 6)
print mul(5, 6)
}</lang>
}</lang>

=={{header|Bash}}==
return an exit code
<lang bash>multiply() {
return $(($1 * $2))
}

multiply 5 6
echo $?</lang>
echo the result
<lang bash>multiply() {
echo -n $(($1 * $2))
}

echo $(multiply 5 6)</lang>


=={{header|BASIC}}==
=={{header|BASIC}}==
Line 143: Line 128:


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).
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#}}==

<lang csharp>static double multiply(double a, double b)
{
return a * b;
}</lang>

Anonymous function:

<lang csharp>Func<double, double, double> multiply = ((a,b) => a*b);</lang>


=={{header|C++}}==
=={{header|C++}}==
Line 164: Line 160:


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.
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.

=={{header|C sharp|C#}}==

<lang csharp>static double multiply(double a, double b)
{
return a * b;
}</lang>

Anonymous function:

<lang csharp>Func<double, double, double> multiply = ((a,b) => a*b);</lang>


=={{header|Clojure}}==
=={{header|Clojure}}==
Line 256: Line 241:
=={{header|Erlang}}==
=={{header|Erlang}}==
<lang erlang>multiply(A,B) -> A*B.</lang>
<lang erlang>multiply(A,B) -> A*B.</lang>

=={{header|F Sharp|F#}}==
The default will be an integer function but you can specify other types as shown:
<lang fsharp>let multiply x y = x * y // integer
let fmultiply (x : float) (y : float) = x * y</lang>


=={{header|Factor}}==
=={{header|Factor}}==
Line 320: Line 310:
end function multiply
end function multiply
end module elemFunc</lang>
end module elemFunc</lang>

=={{header|F Sharp|F#}}==
The default will be an integer function but you can specify other types as shown:
<lang fsharp>let multiply x y = x * y // integer
let fmultiply (x : float) (y : float) = x * y</lang>


=={{header|GML}}==
=={{header|GML}}==
Line 356: Line 341:
multiply = a * b
multiply = a * b
END</lang>
END</lang>

=={{header|Icon}} and {{header|Unicon}}==
{{works with|Unicon}}
<lang Icon>procedure multiply(a,b)
return a * b
end</lang>


=={{header|IDL}}==
=={{header|IDL}}==
Line 382: Line 373:


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.
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|Icon}} and {{header|Unicon}}==
{{works with|Unicon}}
<lang Icon>procedure multiply(a,b)
return a * b
end</lang>


=={{header|Inform 7}}==
=={{header|Inform 7}}==
Line 430: Line 415:
output :x * :y
output :x * :y
end</lang>
end</lang>
=={{header|LSE64}}==
<lang lse64>multiply : *
multiply. : *. # floating point</lang>

=={{header|Lua}}==
=={{header|Lua}}==
<lang Lua>function multiply( a, b )
<lang Lua>function multiply( a, b )
Line 437: Line 426:
=={{header|Lucid}}==
=={{header|Lucid}}==
<lang lucid>multiply(x,y) = x * y</lang>
<lang lucid>multiply(x,y) = x * y</lang>

=={{header|LSE64}}==
<lang lse64>multiply : *
multiply. : *. # floating point</lang>


=={{header|M4}}==
=={{header|M4}}==
Line 957: Line 942:
=={{header|Toka}}==
=={{header|Toka}}==
<lang toka>[ ( ab-c ) * ] is multiply</lang>
<lang toka>[ ( ab-c ) * ] is multiply</lang>

=={{header|UNIX Shell}}==
{{works with|Bash}}

return an exit code
<lang bash>multiply() {
return $(($1 * $2))
}

multiply 5 6
echo $?</lang>
echo the result
<lang bash>multiply() {
echo -n $(($1 * $2))
}

echo $(multiply 5 6)</lang>


=={{header|Ursala}}==
=={{header|Ursala}}==