Return multiple values: Difference between revisions

m (→‎{{header|Clojure}}: Formatting error.)
Line 498:
Mercury is a logic language. Its unification semantics permit any number of output parameters (the closest equivalent to return values). The sample code provided here centres on the <code>addsub/4</code> predicate. The <code>mode</code> statement identifies the first two parameters as input parameters and the last two as output parameters, thus, in effect, returning two results. In this case the first output parameter returns the sum of the two inputs and the second output returns the difference of the two inputs.
===addsub.m===
<lang mercury>:- module addsub.
:- module addsub.
 
:- interface.
Line 508 ⟶ 509:
 
main(!IO) :-
io.command_line_arguments(Args, !IO),
list.filter_map(string.to_int, Args, CleanArgs),
(list.length(CleanArgs, 2) ->
X = list.det_index1(CleanArgs,1),
Y = list.det_index1(CleanArgs,2),
addsub(X, Y, S, D),
io.format("%d + %d = %d\n%d - %d = %d\n",
[i(X), i(Y), i(S), i(X), i(Y), i(D)], !IO)
;
io.write_string("Please pass two integers on the command line.\n", !IO)
).
 
:- pred addsub(int::in, int::in, int::out, int::out) is det.
:- mode addsub(in, in, out, out) is det.
addsub(X, Y, S, D) :-
S = X + Y,
D = X - Y.</lang>
</lang>
 
===Use and output===
<pre><nowiki>$ mmc addsub.m -E && ./addsub 100 999